asp.net连接数据库出错
本人刚学.net看着教程打下了下面这个例子,主要作用是想数据库中添加数据,但我在调试时总有小问题:问题如图所示:下面就是我打的代码了,调试时说是最后一行有问题了,//验证...
本人刚学.net看着教程打下了下面这个例子, 主要作用是想数据库中添加数据,但我在调试时总有小问题:
问题如图所示:
下面就是我打的代码了, 调试时说是最后一行有问题了,
//验证数据的都省略了//
SqlConnection conn = new SqlConnection();
SqlDataAdapter da = new SqlDataAdapter();
DataSet ds = new DataSet(); //创建数据集对象
conn.ConnectionString = "Data Source=YANGXIAODON;initial catalog=bookshop;uid=sa;pwd=;";
SqlCommand selcmd = new SqlCommand();
selcmd.CommandText = "select *from bookshop"; //命令文本
selcmd.Connection = conn; // 发送的连接
SqlCommand inscmd = new SqlCommand();
inscmd.CommandText = "insert into bookshop(@bookname,@author,@publisher,@unitprice,@discount,@bookimagename)";
inscmd.Connection = conn;
//向插入命令添加参数
inscmd.Parameters.Add("@bookname", SqlDbType.NVarChar, 50, "bookname"); // 参数集 即~parameters
inscmd.Parameters.Add("@author", SqlDbType.NVarChar, 50, "author");
inscmd.Parameters.Add("@publisher", SqlDbType.NVarChar, 50, "publisher");
inscmd.Parameters.Add("@unitprice", SqlDbType.Money, 8, "unitprice");
inscmd.Parameters.Add("@discount", SqlDbType.NVarChar, 8, "discount");
inscmd.Parameters.Add("@bookimagename", SqlDbType.NVarChar, 50, "bookimagename");
da.SelectCommand = selcmd;
da.InsertCommand = inscmd;
da.Fill(ds, "books");
//向记录集中添加一条记录
DataRow drnew = ds.Tables["books"].NewRow();
drnew[1] = TextBox1.Text;
drnew[2] = TextBox2.Text;
drnew[3] = TextBox3.Text;
drnew[4] = TextBox4.Text;
drnew[5] = TextBox5.Text;
drnew[6] = TextBox6.Text;
//向表中添加行
ds.Tables["books"].Rows.Add(drnew);
da.Update(ds, "books"); 展开
问题如图所示:
下面就是我打的代码了, 调试时说是最后一行有问题了,
//验证数据的都省略了//
SqlConnection conn = new SqlConnection();
SqlDataAdapter da = new SqlDataAdapter();
DataSet ds = new DataSet(); //创建数据集对象
conn.ConnectionString = "Data Source=YANGXIAODON;initial catalog=bookshop;uid=sa;pwd=;";
SqlCommand selcmd = new SqlCommand();
selcmd.CommandText = "select *from bookshop"; //命令文本
selcmd.Connection = conn; // 发送的连接
SqlCommand inscmd = new SqlCommand();
inscmd.CommandText = "insert into bookshop(@bookname,@author,@publisher,@unitprice,@discount,@bookimagename)";
inscmd.Connection = conn;
//向插入命令添加参数
inscmd.Parameters.Add("@bookname", SqlDbType.NVarChar, 50, "bookname"); // 参数集 即~parameters
inscmd.Parameters.Add("@author", SqlDbType.NVarChar, 50, "author");
inscmd.Parameters.Add("@publisher", SqlDbType.NVarChar, 50, "publisher");
inscmd.Parameters.Add("@unitprice", SqlDbType.Money, 8, "unitprice");
inscmd.Parameters.Add("@discount", SqlDbType.NVarChar, 8, "discount");
inscmd.Parameters.Add("@bookimagename", SqlDbType.NVarChar, 50, "bookimagename");
da.SelectCommand = selcmd;
da.InsertCommand = inscmd;
da.Fill(ds, "books");
//向记录集中添加一条记录
DataRow drnew = ds.Tables["books"].NewRow();
drnew[1] = TextBox1.Text;
drnew[2] = TextBox2.Text;
drnew[3] = TextBox3.Text;
drnew[4] = TextBox4.Text;
drnew[5] = TextBox5.Text;
drnew[6] = TextBox6.Text;
//向表中添加行
ds.Tables["books"].Rows.Add(drnew);
da.Update(ds, "books"); 展开
3个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询