ASP.net数据库代码使用c#
请告诉我此代码中的错误,数据未存储在表中ASP.net数据库代码使用c#,c#,asp.net,sql-server,database,ado.net,C#,Asp.net,Sql Server,Database,Ado.net,请告诉我此代码中的错误,数据未存储在表中 SqlConnection con; SqlCommand com; string constr = @"Data Source=SQL5004.myASP.NET;Initial Catalog=DB_9F2D70_arjunb98;User Id=DB_9F2D70_arjunb98_admin;Password=#;"; protected void submit_Click(object sender, EventArgs e) {
SqlConnection con;
SqlCommand com;
string constr = @"Data Source=SQL5004.myASP.NET;Initial Catalog=DB_9F2D70_arjunb98;User Id=DB_9F2D70_arjunb98_admin;Password=#;";
protected void submit_Click(object sender, EventArgs e)
{
try
{
con = new SqlConnection(constr);
con.Open();
com = new SqlCommand();
com.CommandText = "insert into table values ('" + DropDownList2.SelectedValue + "', '" + DropDownList1.SelectedValue + "','" + name.Text + "','" + mail.Text + "', '" + Address.Text + "', '" + DropDownList3.SelectedValue + "', '" + ph.Text + "','" + message.Text + "')";
com.Connection = con;
com.ExecuteNonQuery();
}
catch (Exception ex)
{
Page.ClientScript.RegisterStartupScript(
Page.GetType(),
"MessageBox", "<script language='javascript'>alert('Sorry! the data is not Submitted, Please try again ')</script>");
}
finally
{
con.Close();
}
SqlConnection-con;
SqlCommand-com;
字符串constr=@“数据源=SQL5004.myASP.NET;初始目录=DB_9F2D70_arjunb98;用户Id=DB_9F2D70_arjunb98_admin;密码=#;”;
受保护的无效提交\u单击(对象发送者,事件参数e)
{
尝试
{
con=新的SqlConnection(cont);
con.Open();
com=newsqlcommand();
com.CommandText=“插入表值(“+DropDownList2.SelectedValue+”、“+DropDownList1.SelectedValue+”、“+name.Text+”、“+mail.Text+”、“+Address.Text+”、“+DropDownList3.SelectedValue+”、“+ph.Text+”、“+message.Text+”);
com.Connection=con;
com.ExecuteNonQuery();
}
捕获(例外情况除外)
{
Page.ClientScript.RegisterStartupScript(
Page.GetType(),
“MessageBox”、“警报('抱歉!数据未提交,请重试')”;
}
最后
{
con.Close();
}
aahhhh。你把代码弄乱了。而且你还没有显示你的aspx
,所以我不太清楚你正在使用的参数
正如Tim所建议的那样,你的代码很容易出错。因此最好使用(在你的情况下建议使用)
因此,代码的简单参考如下:-
在按钮上单击
您可以编写如下内容
protected void submit_Click(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection("Data Source=SQL5004.myASP.NET;Initial Catalog=DB_9F2D70_arjunb98;User Id=DB_9F2D70_arjunb98_admin;Password=#;"); // connection string described by you
SqlCommand cmd = new SqlCommand("insert into Person(Column1,Column2,Column3,....) values(@Column1, @Column2, @Column3,...)", conn);
cmd.Parameters.AddWithValue("@Column1", TextBoxID.text); // it may be option as per your requirement(ex:- Dropdownlist)
cmd.Parameters.AddWithValue("@Column2", textbox2.Text);
cmd.Parameters.AddWithValue("@Column3", textbox3.Text);
try
{
conn.Open();
cmd.ExecuteNonQuery();
}
catch
{
label.Text = "Error when saving on database";
conn.Close();
}
// Empty your controls here
Control1.text = "";
Control2.text = "";
Control3.text = "";
}
还有许多其他链接可供您选择
希望这也能帮助你澄清你的疑问
愉快学习:)1.SQL注入漏洞。2.您正在吞咽异常-对其执行某些操作,以便了解实际错误。3.我99.9%确定您必须指定列以及值-即使您不指定,但最好这样做-如果您稍后向表中添加列会发生什么情况?4。
table
是一个SQL保留字-您真的将表命名为“table”了吗?全部奇怪的代码…在一句话中哪一行代码是针对ASP.NET的?我们不关心代码中运行的内容-如果它与问题无关,请删除标记。您还可以使用“谢谢”来回答问题answer@user3332694:如果它对您有帮助,也请为其他人接受:)您好,先生,非常感谢,您的代码运行得非常好。@user3332694:如果有效,请勾选答案。这样对其他人会有帮助