用textbox录入数据库不成功!
代码能够运行.但我点击后,不能将数据录入到库中.
诸位帮我看看错在哪里?还是应该加东西那?
<%@ Import Namespace= "System.Data " %>
<%@ Import Namespace= "System.Data.SqlClient "%>
<html>
<head>
<title> 录入数据 </title>
</head>
<body style= "text-align: center ">
<form id= "form1 " runat= "server " m>
<asp:TextBox ID= "TextBox1 " runat= "server "/>
<asp:Button ID= "Button1 " runat= "server " Text= "提交 " OnClick= "Button1_Click " />
</form>
</body>
</html>
<script language= "c# " runat= "server ">
void Button1_Click(object sender, EventArgs e)
{
string con = "server=localhost;user id=sa;password=;database=base1 ";
SqlConnection SqlConnection1 = new SqlConnection(con);
SqlConnection1.Open( );
SqlCommand SqlCommand1 = new SqlCommand( "APP ",SqlConnection1);
SqlCommand1.CommandType = CommandType.StoredProcedure;
SqlParameter par1 = new SqlParameter( "@roots ", SqlDbType.VarChar,8);
par1.Value = TextBox1.Text;
SqlCommand1.Parameters.Add(par1);
}
</script>
存储过程为:
CREATE PROCEDURE APP
@roots varchar(8)
as
insert into table2 (roots) values( '@roots ')
GO
[解决办法]
SqlCommand1.ExecuteNonQuery();
缺少了
[解决办法]
void Button1_Click(object sender, EventArgs e)
{
string con = "server=localhost;user id=sa;password=;database=base1 ";
SqlConnection SqlConnection1 = new SqlConnection(con);
SqlConnection1.Open( );
SqlCommand SqlCommand1 = new SqlCommand( "APP ",SqlConnection1);
SqlCommand1.CommandType = CommandType.StoredProcedure;
SqlParameter par1 = new SqlParameter( "@roots ", SqlDbType.VarChar,8);
par1.Value = TextBox1.Text;
SqlCommand1.Parameters.Add(par1);
SqlCommand1.ExecuteNonQuery(); //这一句不能少, 没有执行怎么会运行呢:)。
SqlConnection1.Close(); //链接是很消耗资源的,使用后应该及时关闭
}
CREATE PROCEDURE APP
@roots varchar(8)
as
insert into table2 (roots) values(@roots)
--在存储过程中不能在为字符串加引号
GO