ASP.NET+C# 连接SQL server数据库的问题

来源:百度知道 编辑:UC知道 时间:2024/05/22 04:43:39
我要做一个界面,在此界面进行点播歌曲信息的添加,点击“点播”按钮是,使添加的信息传送到数据库,可我调试好久也不能把信息输入数据库,总是显示“点播失败!”。求高手们给我看看是什么问题,谢谢了。
代码如下:
protected void LinkButton1_Click(object sender, EventArgs e)
{
if (IsValid)
{

string connStr = System.Configuration.ConfigurationManager.AppSettings["sqlconnectionstring"];
SqlConnection myconnection = new SqlConnection(connStr);
string insertStr = "INSERT INTO requestInfo" + "(ID,ToID,FromID,Remark,SentTime) VALUES ('" + TextBox1.Text + "','" + TextBox2.Text + "','" + TextBox4.Text + "','" + TextBox5.Text + "')";
SqlCommand mycommand = new SqlCommand(insertStr, myconnection);
try
{
myconnection.Open();
mycommand.ExecuteNonQuery();
Label5.Text = "点播成功!"

你的SQL语句错了,在值里面少了一个string insertStr = "INSERT INTO requestInfo" + "(ID,ToID,FromID,Remark,SentTime) VALUES ('" + TextBox1.Text + "','" + TextBox2.Text + "','" + TextBox3.Text + "','" + TextBox4.Text + "','" + TextBox5.Text + "')";

string connStr = System.Configuration.ConfigurationManager.AppSettings["sqlconnectionstring"];
是这一句话有问题吧,没有打开数据库吧

1,你少了一个TextBox3.Text,设定的参数数量和传递的参数数量不符。
2,ID,ToID,FromID是char型的还是int型的,如果是int型的需要进行转换。比如Convert.ToInt32(TextBox1.Text ),同时把左右的引号去掉。
3.同样,如果SentTime是DateTime型的,也需要对TextBox5.Text进行转换,Convert.ToInt32(TextBox5.Text );

ID字段如果是自动增长的话就不用写,如果不是,就是你的参数数量和传递的参数数量不符了,最好在检查一下数据类型是否相符合。出于安全的考虑最好用参数进行传参,这样就避免了别人对你的数据库进行注入式攻击了.....如果再不行,你就应该自己设置断点,并添加监视对程序进行跟踪了。祝你好运!

看这里
##
string insertStr = "INSERT INTO requestInfo" + "(ID,ToID,FromID,Remark,SentTime) VALUES ('" + Tex