1

我写了这段代码但是没有在数据库中列出会显示成功插入的消息吗?

if (txtdate.Text != "" && txtalireza.Text != "" && txtdatekamel.Text != "" && txthasan.Text != "" && txtkarbar.Text != "" && txtkarkard.Text != "" && txtkharid.Text != "" && txtmoj.Text != "" && txttozih.Text != "")
            {

                SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirector  y|\Resources\dbcn.mdf;Integrated Security=True;User Instance=True");
                SqlCommand com = new SqlCommand();
                com.Connection = con;
                com.CommandText="INSERT INTO newbuy(byname,datekol,dateshort,karkard,kharidha,b  arali,barhasan,barmoj,descrip)VALUES(@byname,@date  kol,@dateshort,@karkard,@kharidha,@barali,@barhasa  n,@barmoj,@descrip)";
                com.Parameters.AddWithValue("@byname",txtkarbar.Te  xt);
                com.Parameters.AddWithValue("@datekol", txtdatekamel.Text);
                com.Parameters.AddWithValue("@dateshort", txtdate.Text);
                com.Parameters.AddWithValue("@karkard",txtkarkard.  Text);
                com.Parameters.AddWithValue("@kharidha",txtkharid.  Text);
                com.Parameters.AddWithValue("@barali",txtalireza.T  ext);
                com.Parameters.AddWithValue("@barhasan",txthasan.T  ext);
                com.Parameters.AddWithValue("@barmoj",txtmoj.Text)  ;
                com.Parameters.AddWithValue("@descrip",txttozih.Te  xt);
                con.Open();
                com.ExecuteNonQuery();

                MessageBox.Show("inserted");

                con.Close();
            }
            else
            { MessageBox.Show("error"); }`
4

2 回答 2

2
int result=0;    
SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirector  y|\Resources\dbcn.mdf;Integrated Security=True;User Instance=True");
            SqlCommand com = new SqlCommand();
            com.Connection = con;
            com.CommandText="INSERT INTO newbuy(byname,datekol,dateshort,karkard,kharidha,b  arali,barhasan,barmoj,descrip)VALUES(@byname,@date  kol,@dateshort,@karkard,@kharidha,@barali,@barhasa  n,@barmoj,@descrip)";
            com.Parameters.AddWithValue("@byname",txtkarbar.Te  xt);
            com.Parameters.AddWithValue("@datekol", txtdatekamel.Text);
            com.Parameters.AddWithValue("@dateshort", txtdate.Text);
            com.Parameters.AddWithValue("@karkard",txtkarkard.Text);
            com.Parameters.AddWithValue("@kharidha",txtkharid.Text);
            com.Parameters.AddWithValue("@barali",txtalireza.Text);
            com.Parameters.AddWithValue("@barhasan",txthasan.Text);
            com.Parameters.AddWithValue("@barmoj",txtmoj.Text)  ;
            com.Parameters.AddWithValue("@descrip",txttozih.Text);
            try
            {
               con.Open();
               result=Convert.ToInt32(com.ExecuteNonQuery());
               if(result > 0)
               {
               // Success (show your message)
               }
            }
            catch(SqlException ex)
            {
               // Log exception 
               throw ex; 
            }
            finally
            {
              con.Close();
            }
于 2012-10-18T08:30:05.333 回答
1

remove space从值括号

com.CommandText="INSERT INTO newbuy(byname,datekol,dateshort,karkard,kharidha,b arali,barhasan,barmoj,descrip)VALUES(@byname,@date kol,@dateshort,@karkard,@kharidha,@barali,@barhasa n,@barmoj,@descrip)";到@date 和 kol 之间

com.CommandText="INSERT INTO newbuy(byname,datekol,dateshort,karkard,kharidha,b  arali,barhasan,barmoj,descrip)VALUES(@byname,@datekol,@dateshort,@karkard,@kharidha,@barali,@barhasa  n,@barmoj,@descrip)";
于 2012-10-18T08:29:24.667 回答