问题描述
我正在使用此代码将数据存储到sql server中的表中。但它显示了一个错误:C#代码在数据库中插入数据。
这是我的代码:
使用System.Data .SqlClient;
protected void Button1_Click(object sender,EventArgs e)
{
SqlConnection cnn = new SqlConnection();
cnn.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings [sqlconnection] .ConnectionString;
cnn.Open ();
SqlCommand cmd = new SqlCommand();
cmd.CommandText =select * from TableName;
cmd.Connection = cnn;
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = cmd;
DataSet ds = new DataSet();
da.Fill(ds,TableName);
SqlCommandBuilder cb = new SqlCommandBuilder(da);
DataRow drow = ds.Tables [ TableName] .NewRow();
drow [id] = TextBox1.Text;
drow [Name] = TextBox2.Text;
drow [Designation] = TextBox3.Text;
drow [Mobile_No] = TextBox4.Text;
drow [地址] = TextBox5.Text;
ds.Tables [TableName] .Rows.Add(drow);
da.Update(ds,TableName);
string script = @ < script language =javascript>
alert(''信息已成功保存....... !!!!!。'');
< / script> ;;;
Page.ClientScript.RegisterStartupScript(this.GetType(),myJScript1,脚本);
}
//在web.config中
I am using this code to store data into a table in sql server. But it shows me an error:"C# code to insert data in database".
Here is my code:
using System.Data.SqlClient;
protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection cnn = new SqlConnection();
cnn.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["sqlconnection "].ConnectionString;
cnn.Open();
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "select * from TableName";
cmd.Connection = cnn;
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = cmd;
DataSet ds = new DataSet();
da.Fill(ds, " TableName ");
SqlCommandBuilder cb = new SqlCommandBuilder(da);
DataRow drow = ds.Tables["TableName"].NewRow();
drow["id"] = TextBox1.Text;
drow["Name"] = TextBox2.Text;
drow["Designation"] = TextBox3.Text;
drow["Mobile_No"] = TextBox4.Text;
drow["Address"] = TextBox5.Text;
ds.Tables["TableName "].Rows.Add(drow);
da.Update(ds, " TableName ");
string script = @"<script language=""javascript"">
alert(''Information have been Saved Successfully.......!!!!!.'');
</script>;";
Page.ClientScript.RegisterStartupScript(this.GetType(), "myJScript1", script);
}
//In web.config
<connectionStrings>
<add name="sqlconnection" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True" providerName="System.Data.SqlClient"/>
</connectionStrings>
Please Help me....
推荐答案
这篇关于ASP.NET与Sql Server(数据存储)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!