2013-07-10 121 views
-1

非常新的编码,我基本上试图创建一个asp网站,将显示在网格中的数据,我工作得很好(很简单),但我想能够添加它从Asp.net网页插入数据到本地SQL express数据库

我有一个default.aspx,其中我的数据是介绍和一个Add.aspx是我的插入页面。

我有4个文本框,我用键入数据,并在一个按钮,它会插入数据到SQL数据库表达我已经运行的推动下,

进出口运行,1 DB 1台,所以你可以理解我的小项目是多么简单。

我跑调试与我没有问题:

为Default.aspx的

</title> 
</head> 
<body> 
    <strong style="border-style: ridge">:: Server List Details Page ::</strong> 
<p></p> 


    <form id="form1" runat="server"> 
    <div> 
    </div> 

      <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
     DataSourceID="SqlDataSource1" AllowSorting="True"> 
       <Columns> 
        <asp:BoundField DataField="ServerName" HeaderText="ServerName" 
         SortExpression="ServerName" /> 
        <asp:BoundField DataField="IPAddress" HeaderText="IPAddress" 
         SortExpression="IPAddress" /> 
        <asp:BoundField DataField="Description" HeaderText="Description" 
         SortExpression="Description" /> 
        <asp:BoundField DataField="Location" HeaderText="Location" 
         SortExpression="Location" /> 
       </Columns> 
      </asp:GridView> 
      <asp:Button ID="Add" runat="server" Text="Add Device" OnClick="Add_click" OnClientClick="window.open('add.aspx', 'add');"/> 

    <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
     ConnectionString="<%$ ConnectionStrings:ConnectionString %>" 

     SelectCommand="SELECT [ServerName], [IPAddress], [Description], [Location] FROM [Table1]"> 
    </asp:SqlDataSource> 
    </form> 


</body> 
</html> 

代码代码ADD.aspx

<title></title> 
    </head> 
<body> 
    <form id="form1" runat="server"> 
    <strong>:: Add Device Details :: 
    <br /> 
    <br /> 
    <br /> 
    </strong> 

    Server Name: 
    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> 
    <br /> 
    IP Address:&nbsp;&nbsp;&nbsp; 
    <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox> 
    <br /> 
    Description:&nbsp;&nbsp;&nbsp; 
    <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox> 
    <br /> 
    Location:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
    <asp:TextBox ID="TextBox4" runat="server"></asp:TextBox> 
    <br /> 
    <br /> 
    <asp:Button ID="Generate_Data" runat="server" Text="Generate Data" 
     onclick="Generate_Data_Click" PostBackUrl="~/Default.aspx" /> 
    </form> 





</body> 
</html> 

代码按钮提交信息:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using System.Data.SqlClient; 


namespace Table_to_DB 
{ 
    public partial class WebForm2 : System.Web.UI.Page 
    { 
     protected void Page_Load(object sender, EventArgs e) 
     { 

     } 

     protected void Generate_Data_Click(object sender, EventArgs e) 
     { 
      SqlConnection cn = new SqlConnection("Data Source= 172.17.10.89; initial catalog=database1"); 

      SqlCommand cmd = new SqlCommand("INSERT INTO database1 (" + " servername, ipaddress, description, Location " + ") VALUES (" + " @TextBox1.text, @TextBox2.text, @TextBox3.text, @TextBox4.text" + ")", cn); 

      cn.Open(); 
      cmd.ExecuteNonQuery(); 
      cn.Close(); 

     } 
    } 
} 

遇到的问题即时消息是,当我输入我的数据并提交,没有任何错误和没有数据结束桌子。

请大家帮忙,这段代码大部分都是网上下的,我还在学习。

回答

2

您的命令文本错误。将参数传递给一个SqlCommand应该是这个

string cmdText = "INSERT INTO table1 (servername, ipaddress, description, Location) " + 
       "VALUES (@p1, @p2, @p3, @p4)"; 
string conString = "Data Source= 172.17.10.89; initial catalog=database1"; 

using(SqlConnection cn = new SqlConnection(conString)) 
using(SqlCommand cmd = new SqlCommand(cmdText, cn)) 
{ 
    cn.Open(); 
    cmd.Parameters.AddWithValue("@p1",TextBox1.Text); 
    cmd.Parameters.AddWithValue("@p2",TextBox2.Text); 
    cmd.Parameters.AddWithValue("@p3",TextBox3.Text); 
    cmd.Parameters.AddWithValue("@p4",TextBox4.Text); 
    int recordsAdded = cmd.ExecuteNonQuery(); 
} 

你不能直接在文本框的属性文本传递到命令文本。
在这里你设置了一些占位符(我已经调用了泛型@ p1等,但是你可以改变为你喜欢的更精确的名称),然后你为每个占位符的SqlCommand设置的Parameters集合准备好你想要的值存储在数据库中。
另外我建议给你的文本框更多有意义的名字。

并保持使用参数始终。不要使用字符串连接。它看起来可能更快,但它是通往地狱的道路

+0

+5绝对,最后一句粗体。 –

+0

仍然没有运气,它似乎没有创建连接到数据库的所有...没有调试错误,也在@ p4后)“;西米冒号错误即时假设应该有一个)后” 是我的意思是编辑我的任何代码来适应那些p1,p2,p3,p4 –

+0

是的,你是对的,忘记了SqlCommand创建和连接的花括号。如果您在连接服务器时遇到问题,应引发异常。在调用此方法的代码中是否有任何try/catch?如果是,请不要吞下异常并尝试写入收到的错误消息,如果不尝试在此处添加某些内容以打印消息。还添加了插入记录的计数器。它应该是1 – Steve

相关问题