简体   繁体   中英

Trying to put data into a SQL Server database from textbox and some data from another table in C#

I am trying to enter some data into table2 from textboxes and some data from A SQL Server database table1 , but all logic failed. Please help.

Here is the code:

string qry = "insert into sale (inno,date,type,custname,item,qty,price,dis,disa,gst,gsta,total) values (" + invoice + ",'" + dt + "','" + paytype + "','" + custname + "','" + item + "'," + iqty + "," + iprice + "," + idisc + "," + idsa + "," + igst + "," + igsta + "," + itot + ") and insert into sale (hsn,sgst,cgst) select hsn,sgst,cgst from stock";

SqlCommand sc = new SqlCommand(qry, con);

int i = sc.ExecuteNonQuery();

if (i >= 1)
    MessageBox.Show("Inserted successfully");
else
    MessageBox.Show("Not done");

There is no way to see what happened here from the info you have shared, for the very least you should write what result or what exception you got and what db you are using. Despite that, I would bet that at least some of the columns you are trying to populate in the table are from the type varchar (or string or whatever), and you didn't put a value inside ' ' or didn't properly escaped your strings (if one of the variable contains the ' sign your query would fail and possibly destroy your DB with sql injection).
I could also mention the fact that the code is unmaintainable and some other possible problems, but it's not the time or place for that.

首先,从表(使用SQL)和/或TextBox加载数据,然后将其存储到本地变量中,然后使用SQL将其插入所需的表中。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM