如何将多个图像添加到表
问题描述:
如何将多个图像添加到表中?这里的test_table表具有两个nvarchar字段(名称,聚会)和两个图像字段(party_im,name_im).这段代码不会导致任何错误,但是没有将数据插入表中?有人可以找到该代码有什么问题吗?
请帮帮我.
How to add multiple images into a table? Here test_table table which has two nvarchar fields (name, party) and two image fields(party_im,name_im). This code does not cause for any errors but data is not inserted in to the table?can any one please find what is wrong with the code?
Please help me.
string dbconnstr = @"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\compaq\Desktop\new\n1\ClassLibrary1\Database1.mdf;Integrated Security=True;User Instance=True";
SqlConnection conn = new SqlConnection(dbconnstr);
string qry = "insert into test_table (name,party,party_im,name_im) values (textBox1.Text,textBox2.Text,@ImageData1,@ImageData2)";
//Initialize SqlCommand object for insert.
SqlCommand SqlCom = new SqlCommand(qry, conn);
SqlCom.Parameters.Add(new SqlParameter("@ImageData1", (object)imagedata));
SqlCom.Parameters.Add(new SqlParameter("@ImageData2", (object)imagedata));
n = -1;
//Open connection and execute insert query.
conn.Open();
n = SqlCom.ExecuteNonQuery();
conn.Close();
conn.Dispose();
MessageBox.Show("Succesfully Inserted");
答
让我猜:表字段名称和参与方包含文本"textBox1.Text"和"textBox2.Text"!
那是因为
Let me guess: the table fields name and party contain the text "textBox1.Text" and "textBox2.Text"!
That is because
SqlConnection conn = new SqlConnection(dbconnstr);
string qry = "insert into test_table (name,party,party_im,name_im) values (textBox1.Text,textBox2.Text,@ImageData1,@ImageData2)";
将文本框的名称硬编码到SQL字符串中,而不是内容中.试试:
Hard codes the names of the textboxes into the SQL string, rather than the contents. Try:
SqlConnection conn = new SqlConnection(dbconnstr);
string qry = "insert into test_table (name, party, party_im, name_im) values (@NM, @PT, @ImageData1, @ImageData2)";
//Initialize SqlCommand object for insert.
SqlCommand SqlCom = new SqlCommand(qry, conn);
SqlCom.Parameters.Add(new SqlParameter("@NM", textBox1.Text));
SqlCom.Parameters.Add(new SqlParameter("@PT", textBox2.Text));
SqlCom.Parameters.Add(new SqlParameter("@ImageData1", imagedata));
SqlCom.Parameters.Add(new SqlParameter("@ImageData2", imagedata));
尽管我为什么不两次向表中插入相同的数据("imagedata"),但我不知道...
Though why you are insertingthe same data to your table twice ("imagedata") I don''t know...