无法将内容插入数据库 [英] Unable to insert contents into the database
问题描述
我在godaddy中创建了一个sql server数据库,并手动创建了一个名为property的表。我也使用连接string成功地将我的应用程序连接到数据库。但是我无法使用我的c#代码向表中插入任何值。下面是我的C#代码
string strQuery =INSERT INTO属性(名称,电子邮件,电话,标题,描述,位置,image1,image2,image3 ,image4)VALUES('+ name +','+ email +','+ phone +','+ title +','+ description +','+ district + ,@数据,@ DATA2,DATA3 @,@ DATA4);;
SqlCommand cmd = new SqlCommand(strQuery);
cmd.Parameters.Add(@ data,SqlDbType.Binary).Value = bytes;
cmd.Parameters.Add(@ data2,SqlDbType.Binary).Value = bytes2;
cmd.Parameters.Add(@ data3,SqlDbType.Binary).Value = bytes3;
cmd.Parameters.Add(@ data4,SqlDbType.Binary).Value = bytes4;
SqlConnection con = new SqlConnection(constr);
cmd.CommandType = CommandType.Text;
cmd.Connection = con;
尝试
{
con.Open();
cmd.ExecuteNonQuery();
返回true;
}
catch(Exception ex)
{
Response.Write(ex.Message);
返回false;
}
最后
{
con.Close();
con.Dispose();
}
当我试图在sql server web admin中插入值时,我收到错误 - 无法加载文件或程序集'Microsoft.SqlServer.BatchParser,Version = 11.0.0.0,Culture = neutral,PublicKeyToken = 89845dcd8080cc91'或其依赖项之一。系统找不到指定的文件。
请帮帮我
首先,停止混合样式并使用参数化查询:串联字符串对SQL注入攻击持开放态度,可能会破坏或破坏您的数据库。无论如何,这可能会解决您的问题。
其次,查看通过catch
获得的任何错误消息 - 它应该为您提供有关什么的线索是错误的。
目前,这里有太多错误的可能性,我们甚至无法猜测你们造成了哪一个更好的信息。
string strQuery = INSERT INTO属性(名称,电子邮件,电话,标题,描述,位置,image1,image2,image3,image4)VALUES(@NM,@ EM ,@ PN,@ HD,@ DS,@ LO,@ D1,@ D2,@ D3,@ D4);
SqlCommand cmd = new SqlCommand(strQuery);
cmd.Parameters.AddWithValue( @ NM,name);
cmd.Parameters.AddWithValue( @ EM,email);
cmd.Parameters.AddWithValue( @ PN,phone);
cmd.Parameters.AddWithValue( @ HD,title);
cmd.Parameters.AddWithValue( @ DS,description);
cmd.Parameters.AddWithValue( @ LO,分区);
cmd.Parameters.AddWithValue( @ D1,bytes);
cmd.Parameters.AddWithValue( @ D2,bytes2);
cmd.Parameters.AddWithValue( @ D3,bytes3);
cmd.Parameters.AddWithValue( @ D4,bytes4);
SqlConnection con = new SqlConnection(constr,con);
尝试
{
...
尝试使用以下链接,可能是您指定的连接字符串的问题。
链接
I have created a sql server database in godaddy and created a table named property manually.i also successfuly connected my application to the database using connection string.But i am unable to insert any values to the table using my c# code Below is my C# code
string strQuery = "INSERT INTO property(name,email,phone,heading,description,location,image1,image2,image3,image4) VALUES('" + name + "','" + email + "','" + phone + "','" + title + "','" + description + "','" + district + "',@data,@data2,@data3,@data4);"; SqlCommand cmd = new SqlCommand(strQuery); cmd.Parameters.Add("@data", SqlDbType.Binary).Value = bytes; cmd.Parameters.Add("@data2", SqlDbType.Binary).Value = bytes2; cmd.Parameters.Add("@data3", SqlDbType.Binary).Value = bytes3; cmd.Parameters.Add("@data4", SqlDbType.Binary).Value = bytes4; SqlConnection con = new SqlConnection(constr); cmd.CommandType = CommandType.Text; cmd.Connection = con; try { con.Open(); cmd.ExecuteNonQuery(); return true; } catch (Exception ex) { Response.Write(ex.Message); return false; } finally { con.Close(); con.Dispose(); }
when i tried to insert value in sql server web admin, i am getting error-Could not load file or assembly 'Microsoft.SqlServer.BatchParser, Version=11.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91' or one of its dependencies. The system cannot find the file specified.
Please help me
First off, stop mixing styles and use parametrized queries throughout: concatenating strings is wide open to SQL injection attack which could damage or destroy your database. This may sovle your problem anyway.
Second, look at any error message you get via thecatch
- it should give you a clue as to what is wrong.
At the moment, there are far too many possibilities for error here, that we can't even begin to guess which one of they you have caused without better information.
string strQuery = "INSERT INTO property(name, email, phone, heading, description, location, image1, image2, image3, image4) VALUES(@NM, @EM, @PN, @HD, @DS, @LO, @D1, @D2, @D3, @D4)"; SqlCommand cmd = new SqlCommand(strQuery); cmd.Parameters.AddWithValue("@NM", name); cmd.Parameters.AddWithValue("@EM", email); cmd.Parameters.AddWithValue("@PN", phone); cmd.Parameters.AddWithValue("@HD", title); cmd.Parameters.AddWithValue("@DS", description); cmd.Parameters.AddWithValue("@LO", district); cmd.Parameters.AddWithValue("@D1", bytes); cmd.Parameters.AddWithValue("@D2", bytes2); cmd.Parameters.AddWithValue("@D3", bytes3); cmd.Parameters.AddWithValue("@D4", bytes4); SqlConnection con = new SqlConnection(constr, con); try { ...
Try with below link, might be an issue with the connection string you specified.
Link
这篇关于无法将内容插入数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!