插入语句 [英] Insert Statement

查看:77
本文介绍了插入语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我试图在数据库中插入值,但出现以下错误:没有为一个或多个必需参数提供值"

//下面是我的代码

Hi

Im trying to insert values in a database but i get the following error:"No value given for one or more required parameters"

//Below is my code

protected void btnAddCourse_Click(object sender, EventArgs e)
   {
       //try
       //{
           string connString = ConfigurationManager.ConnectionStrings["courses"].ConnectionString;
           // set up and open connection
           dbConn = new OleDbConnection(connString);
           dbConn.Open();
           string updateSql = "UPDATE Courses " + "SET coursePrice = @price, StartDate=@date, Duration=@duration, Venue=@venue " + "WHERE Venue = @courseNo";
           dbCmd = new OleDbCommand(updateSql, dbConn);

           dbCmd.Parameters.Add("@price", OleDbType.VarChar, 20, "coursePrice");
           dbCmd.Parameters.Add("@date", OleDbType.VarChar, 20, " StartDate");
           dbCmd.Parameters.Add("@duration", OleDbType.VarChar, 10, "Duration");
           dbCmd.Parameters.Add("@venue", OleDbType.VarChar, 20, " Venue");

           dbCmd.Parameters["@price"].Value = txtPrice.Text;
           dbCmd.Parameters["@date"].Value = txtDate.Text;

           dbCmd.Parameters["@duration"].Value = txtDuration.Text;
           dbCmd.Parameters["@venue"].Value = txtVenue.Text;

           dbCmd.ExecuteNonQuery();
           MessageBox.Show("Added");

       //}
       //catch(OleDbException ex)
       //{
       //    MessageBox.Show(ex.Message);
       //}

   }

推荐答案

string updateSql = "UPDATE Courses " + "SET coursePrice = @price, StartDate=@date, Duration=@duration, Venue=@venue " + "WHERE Venue = @courseNo";

请注意最后一个参数:"@ courseNo".
您在哪里设置?你把所有其他的都设置好了!

顺便说一句:UPDATE命令不添加任何记录-它修改现有记录.因此,您使用的是错误的SQL命令,而是要使用INSERT,或者您的方法名以及消息框是错误的!

Note the final parameter: "@courseNo".
Where do you set it? You set all the others!

BTW: An UPDATE command does not add any records - it modifies an existing one. So you are either using the wrong SQL command and want INSERT instead, or your method name is wrong, as well as your message box!


这篇关于插入语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆