如何插入数据库? [英] how to insert into database ?

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

问题描述

我正在使用此代码插入数据库:

i am using this code to insert into database :

SqlConnection conn = new SqlConnection("Server=XP-PC\\SQLEXPRESS;Database=Warehouse;Trusted_Connection=True");
            string sql = "insert into warehouse_data (Barcode,Product_Name,Amount) values ('" + textBox1.Text + "','" + textBox2.Text + "','" + textBox5.Text + "')";
            SqlCommand cmd = new SqlCommand(sql,conn);
            
            try
            {
                conn.Open();
                cmd.ExecuteScalar();
            }
            catch (SqlException ex)
            {
                MessageBox.Show(ex.ToString());
            }
            conn.Close();




但什么都没发生
为什么?




but nothing happens
why ?
and this is the right code or not ?

推荐答案

使用ExecuteNonQuery代替ExecuteScalar.

我建议您阅读这篇文章: http://blogs.x2line.com/al /archive/2007/05/01/3049.aspx [
Use ExecuteNonQuery instead of ExecuteScalar.

I would recommend you to read this article : http://blogs.x2line.com/al/archive/2007/05/01/3049.aspx[^]

UPDATE 1:

You can give more proper spacing :

string sql = "INSERT INTO WAREHOUSE_DATA(BarCode, Product_Name, Amount) VALUES('" + textBox1.Text + "','" + textBox2.Text + "','" + textBox5.Text + "')";



一些注意事项:
-确保文本框不为空.
-使用SqlParameters代替直接在查询中分配textbox.text.
-有什么异常吗?
-如何检查列的数据类型?

更新2:

OP评论:
文本框不为空,不出现异常,数据类型为(nvarchar(50),nvarchar(50)和int)

我认为您的数据类型有问题.您正在将类型int的字段分配给String.
现在,将查询更改为此:



Some notes :
- Make sure that the textboxes are not empty.
- Use SqlParameters instead directly assigning textbox.text in the query.
- Any Exception?
- How about checking the data types of the columns?

UPDATE 2:

Comment from OP :
text box are not empty , no exception appears , data types are (nvarchar(50),nvarchar(50)and int)

I think there is a problem in your data type. You are assigning the field of type int to String.
Now change your query to this:

string sql = "INSERT INTO WAREHOUSE_DATA(BarCode, Product_Name, Amount) VALUES('" + textBox1.Text + "','" + textBox2.Text + "', int.Parse(textBox5.Text))";

;

现在应该可以了.并使用ExecuteNonQuery ,因为ExecuteScalar 将仅返回一条记录.

;

Now it should work. And do use ExecuteNonQuery as ExecuteScalar will return only one record.


如Tarun所说,
使用ExecuteNonQuery而不是ExecuteScalar.

我只能在您的代码上看到此错误,是的,请注意tarun编写的注释.

试试这个,
As Tarun says ,
Use ExecuteNonQuery instead of ExecuteScalar.

i can see only this error on your code and yes plz take care of notes written by tarun.

try this ,
SqlCommand comm = new SqlCommand("INSERT INTO desg VALUES (@txtBox1, @txtBox2, @txtBox3)", connection);

comm.Parameters.AddWithValue("@txtBox1", textBox1.Text.Trim());
comm.Parameters.AddWithValue("@txtBox2", textBox2.Text.Trim());
comm.Parameters.AddWithValue("@txtBox3", textBox3.Text.Trim());


try
            {
                conn.Open();
                comm.ExecuteNonQuery ();
            }
            catch (SqlException ex)
            {
                MessageBox.Show(ex.ToString());
            }
            conn.Close();



请参阅此链接,它肯定会对您有所帮助.[
^ ]



Refer this link it will definitely help you, [^]


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

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