我无法在我的表中插入一个新元组。它没有给出任何错误,我不知道为什么它不能正常工作。顺便说一句,TextBox3 有类似的东西:C:\Temp\Pic5.jpg。这是我的代码:
protected void Button3_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\MYDB.mdf;Integrated Security=True;User Instance=True");
byte[] data;
try
{
data = System.IO.File.ReadAllBytes(@"" + TextBox3.Text);
string val1 = "" + TextBox1.Text;
string val2 = "" + TextBox2.Text;
string val3 = "" + TextBox3.Text;
SqlCommand cmd = new SqlCommand("Insert INTO Books (Book_Name,Author_Name,Image,In_Lib) VALUES (@BN,@AN,@DATA,@LIB)", con);
cmd.Parameters.AddWithValue("@BN", val1);
cmd.Parameters.AddWithValue("@AN", val2);
cmd.Parameters.AddWithValue("@DATA", val3);
cmd.Parameters.AddWithValue("@LIB", "YES");
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
catch (Exception err)
{
Console.WriteLine(err.Message);
}
}
asp部分
Book Name:
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<br />
Author Name:
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<br />
Image Name:
<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
<br />
<asp:Button ID="Button3" runat="server" onclick="Button3_Click" Text="Submit" />
<br />
我的数据库是这样的:
- 书名 varchar(50)
- Author_Name varchar(50)
- 图像图像
- In_Lib varchar(50)