如何将数据插入SQL表 [英] How to insert data into a SQL table
本文介绍了如何将数据插入SQL表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想知道如何将信息插入到数据库表中,我使用的代码是:
I would like to know how I can insert information into a database table, the code I am using is this:
SqlConnection conn = new SqlConnection(@"Data source = test1234 ; Database=testData ; User Id=testeid ; Password=1234576");
SqlCommand command = new SqlCommand("IF OBJECT_ID('UXCashMaticCancelamento', 'U') IS NULL CREATE TABLE UXCashMaticCancelamento(Data int,Utilizador char(50),Precototal int,TipodeDocumentacao char(50),NumFatura int ,Numserie int ,IDCliente char(50),IDVendedor char(50) ), ;", conn);
conn.Open();
SqlCommand insertCommand = new SqlCommand("INSERT INTO UXCashMaticCancelamento(FirstColumn, SecondColumn) VALUES (@0, @1)", conn);
我的尝试:
What I have tried:
string cmdString="INSERT INTO books (name,author,price) VALUES (@val1, @va2, @val3)";
string connString = "your connection string";
using (SqlConnection conn = new SqlConnection(connString))
{
using (SqlCommand comm = new SqlCommand())
{
comm.Connection = conn;
comm.CommandString = cmdString;
推荐答案
嗯......我首先将参数值添加到命令中:
Well...I'd start by adding the parameter values to your command:
SqlCommand insertCommand = new SqlCommand("INSERT INTO UXCashMaticCancelamento(FirstColumn, SecondColumn) VALUES (@C0, @C1)", conn);
insertCommand.Parameters.AddWithValue("@C0", myValueForTheFirstColumn);
insertCommand.Parameters.AddWithValue("@C1", myValueForTheSecondColumn);
然后打开连接,并执行命令:
Then open the connection, and execute the command:
conn.Open();
insertCommand.ExecuteNonQuery();
insertCommand.Dispose();
conn.Close();
conn.Dispose();
首先你必须在数据库中创建表格
first u have to create the table in database
create table data(
id int identity(1,1) primary key(),
Name varchar(50),
vill varchar(50),
mobile nvarchar(50)
)
aspx.cs页面中的
in aspx.cs page
sqlconnection con=new sqlconncetion(your database connection);
sqlcommand cmd=new sqlcommand("insert into data (Name,Vill,mobile) values(@name,@Vill,@mobile)",con);
cmd.parameters.add("@name",textbox1.text);
cmd.parameters.add("@Vill",textbox2.text);
cmd.parameters.add("@mobile",textbox3.text);
con.open();
cmd.excutenonquery();
con.close();
这篇关于如何将数据插入SQL表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文