ASP.NET插入SQL数据库该怎么写,按按钮的时候将TextBox里面的数据写入。 多谢! 给个代码行吗~~ 万分感谢
protectedvoidButton1_Click(objectsender,EventArgse){SqlConnectioncon=newSqlConnection...
protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection("server=localhost;database=Northwind;Trusted_Connection=SSPI;");
con.Open();
SqlDataAdapter mydata = new SqlDataAdapter();
mydata.InsertCommand = new SqlCommand("insert into shuju (TextBox1,TextBox2,TextBox3,TextBox4,TextBox5,TextBox6,TextBox7)values('" + TextBox1.Text + "','" + TextBox2.Text + "','" + TextBox3.Text + "'," + TextBox4.Text + "'," + TextBox5.Text + "'," + TextBox6.Text + "'," + TextBox7.Text + "')", con);
}
这个是我在网上找的代码,不过不知道“com.ExecuteNoneQuery(); 命令没写”该怎么写。麻烦各位高手给弄整理整理~~感谢了!!!
数据库里的列名也是TextBox1-------7 展开
{
SqlConnection con = new SqlConnection("server=localhost;database=Northwind;Trusted_Connection=SSPI;");
con.Open();
SqlDataAdapter mydata = new SqlDataAdapter();
mydata.InsertCommand = new SqlCommand("insert into shuju (TextBox1,TextBox2,TextBox3,TextBox4,TextBox5,TextBox6,TextBox7)values('" + TextBox1.Text + "','" + TextBox2.Text + "','" + TextBox3.Text + "'," + TextBox4.Text + "'," + TextBox5.Text + "'," + TextBox6.Text + "'," + TextBox7.Text + "')", con);
}
这个是我在网上找的代码,不过不知道“com.ExecuteNoneQuery(); 命令没写”该怎么写。麻烦各位高手给弄整理整理~~感谢了!!!
数据库里的列名也是TextBox1-------7 展开
2个回答
展开全部
string insert = string.Format("insert into 表名Column1,TextBox1.....需要插入的各列的列名) values('{0}','{1}','........)",a,b.......插入的变量值);//a,b是变量里面存放你需要插入数据表的值
SqlCommand insertinto = new SqlCommand(insert, con);//con连接字符串
if ((int)insertinto.ExecuteNonQuery()==1)//ExecuteNonQuery()是返回受影响的行数
{
Response.Write("<script type='text/javascript'>alert('写入成功');</script>");//JS弹出提示框插入成功,
}
SqlCommand insertinto = new SqlCommand(insert, con);//con连接字符串
if ((int)insertinto.ExecuteNonQuery()==1)//ExecuteNonQuery()是返回受影响的行数
{
Response.Write("<script type='text/javascript'>alert('写入成功');</script>");//JS弹出提示框插入成功,
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
楼主看看这样写清晰不,如下是按键事件触发代码:
string strConn = "server=localhost;database=Northwind;Trusted_Connection=SSPI;";
using(SqlConnection conn = new SqlConnection(strConn))
{
string strSql="insert into shuju (TextBox1,TextBox2,TextBox3,TextBox4,TextBox5,TextBox6,TextBox7)values('" + TextBox1.Text + "','" + TextBox2.Text + "','" + TextBox3.Text + "'," + TextBox4.Text + "'," + TextBox5.Text + "'," + TextBox6.Text + "'," + TextBox7.Text + "')";
SqlDataAdapter mydata = new SqlDataAdapter();
mydata.InsertCommand = new SqlCommand(strSql,conn);//这句已经执行数据库插入动作了
}
string strConn = "server=localhost;database=Northwind;Trusted_Connection=SSPI;";
using(SqlConnection conn = new SqlConnection(strConn))
{
string strSql="insert into shuju (TextBox1,TextBox2,TextBox3,TextBox4,TextBox5,TextBox6,TextBox7)values('" + TextBox1.Text + "','" + TextBox2.Text + "','" + TextBox3.Text + "'," + TextBox4.Text + "'," + TextBox5.Text + "'," + TextBox6.Text + "'," + TextBox7.Text + "')";
SqlDataAdapter mydata = new SqlDataAdapter();
mydata.InsertCommand = new SqlCommand(strSql,conn);//这句已经执行数据库插入动作了
}
追问
你好;
为何我将上述代码输入后仍然不能写入到数据库中!!!!
多谢您了!!!
谢谢!!!
追答
你的数据库有连接成功吗,有没报什么异常?
重点检查连接字符串,和Sql插入语句。
如下是测试连接数据库是否成功,如成功则返回true,否则反回false.
public static bool TestAccessSqlDataBase()
{
bool IsConnecting = false;
string strConn = "server=localhost;database=Northwind;Trusted_Connection=SSPI;";
using(SqlConnection testConn = new SqlConnection(strConn))
{
testConn.Open();
IsConnecting = true;
}
return IsConnecting ;
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询