值不在数据库中 [英] Values not goin to Database
本文介绍了值不在数据库中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
protected void Button1_Click(对象发件人,EventArgs e)
{
SqlConnection con = new SqlConnection( @ 数据源= HP-PC\SQLEXPRESS;初始目录=查询;集成安全性=真);
con.Open();
SqlCommand cmd = new SqlCommand( sp_userinformation ,con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue( @ Name,Name);
cmd.Parameters.AddWithValue( @Emailid,Emailid);
cmd.Parameters.AddWithValue( @ Phoneno,Phoneno);
cmd.Parameters.AddWithValue( @ Address,地址);
cmd.Parameters.AddWithValue( @ Pincode,Pincode);
cmd.Parameters.AddWithValue( @ Heard,Heard);
cmd.Parameters.AddWithValue( @ Enquiryabout,EnquiryAbout);
cmd.Parameters.AddWithValue( @ Message,Message);
cmd.ExecuteNonQuery();
con.Close();
}
}
i已为注册页面编写此代码...但值未到达数据库...
我的数据库名称是:查询
表:EnquiryTable
请帮帮我.. 。
ima beginer并希望获得指导。
解决方案
我认为你对存储过程感到困惑。用简单的查询替换存储过程参数。
受保护 void Button1_Click( object sender,EventArgs e)
{
尝试
{
SqlConnection con = new SqlConnection( @ 数据源= HP-PC\SQLEXPRESS;初始目录=查询;集成安全性=真);
con.Open();
string query = 插入EnquiryTable值(' + txtName.Text + ',' + txtEmailid.Text + ',' + txtPhoneno.Text + ',' + txtAddress.Text + ',' + txtPincode.Text + ',' + txtHeardfrom.Text + ',' + txtEnquiryabout.Text + ',' + txtMessage.Text + ')跨度>;
SqlCommand cmd = new SqlCommand(queryString,con);
cmd.CommandType = CommandType.Text;
cmd.ExecuteNonQuery();
con.Close();
}
catch (例外情况)
{
}
}
更多信息请参考链接
** 到清楚了解STORED PROCEDURES检查这个链接
参考此链接
使用存储过程与ado.net。
请添加如下的try catch块
受保护 void Button1_Click( object sender,EventArgs e)
{
try
{
SqlConnection con = new SqlConnection( @ 数据源= HP-PC\SQLEXPRESS;初始目录=查询;集成安全性=真);
con.Open();
SqlCommand cmd = new SqlCommand( sp_userinformation ,con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue( @ Name,Name);
cmd.Parameters.AddWithValue( @Emailid,Emailid);
cmd.Parameters.AddWithValue( @ Phoneno,Phoneno);
cmd.Parameters.AddWithValue( @ Address,地址);
cmd.Parameters.AddWithValue( @ Pincode,Pincode);
cmd.Parameters.AddWithValue( @ Heard,Heard);
cmd.Parameters.AddWithValue( @ Enquiryabout,EnquiryAbout);
cmd.Parameters.AddWithValue( @ Message,Message);
cmd.ExecuteNonQuery();
con.Close();
}
catch (例外情况)
{
}
}
然后在调试模式下运行此程序,然后您将收到确切的错误消息,以帮助您自己解决此问题。
protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(@"Data Source=HP-PC\SQLEXPRESS;Initial Catalog=Enquiry;Integrated Security=True");
con.Open();
SqlCommand cmd = new SqlCommand("sp_userinformation", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@Name", Name);
cmd.Parameters.AddWithValue("@Emailid", Emailid);
cmd.Parameters.AddWithValue("@Phoneno", Phoneno);
cmd.Parameters.AddWithValue("@Address", Address);
cmd.Parameters.AddWithValue("@Pincode", Pincode);
cmd.Parameters.AddWithValue("@Heard", Heard);
cmd.Parameters.AddWithValue("@Enquiryabout", EnquiryAbout);
cmd.Parameters.AddWithValue("@Message", Message);
cmd.ExecuteNonQuery();
con.Close();
}
}
i had written this code for registration page...but values are not getting to database...
my database name is: Enquiry
table: EnquiryTable
please help me out...
i m a beginer and want guidance.
解决方案
I think you are getting confused with stored procedure. Replace stored procedure parameters with simple query.
protected void Button1_Click(object sender, EventArgs e) { try { SqlConnection con = new SqlConnection(@"Data Source=HP-PC\SQLEXPRESS;Initial Catalog=Enquiry;Integrated Security=True"); con.Open(); string query="insert into EnquiryTable values('" + txtName.Text + "','" + txtEmailid.Text + "','" + txtPhoneno.Text + "','" + txtAddress.Text + "','" + txtPincode.Text + "','" + txtHeardfrom.Text + "','" + txtEnquiryabout.Text + "','" + txtMessage.Text + "')"; SqlCommand cmd = new SqlCommand(queryString, con); cmd.CommandType = CommandType.Text; cmd.ExecuteNonQuery(); con.Close(); } catch(exception ex) { } }
Fore more information refer this link
** To Get clear idea about STORED PROCEDURES check this link
Refer this link
for Using stored procedures with ado.net.
Please add a try catch block like below
protected void Button1_Click(object sender, EventArgs e) { try { SqlConnection con = new SqlConnection(@"Data Source=HP-PC\SQLEXPRESS;Initial Catalog=Enquiry;Integrated Security=True"); con.Open(); SqlCommand cmd = new SqlCommand("sp_userinformation", con); cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.AddWithValue("@Name", Name); cmd.Parameters.AddWithValue("@Emailid", Emailid); cmd.Parameters.AddWithValue("@Phoneno", Phoneno); cmd.Parameters.AddWithValue("@Address", Address); cmd.Parameters.AddWithValue("@Pincode", Pincode); cmd.Parameters.AddWithValue("@Heard", Heard); cmd.Parameters.AddWithValue("@Enquiryabout", EnquiryAbout); cmd.Parameters.AddWithValue("@Message", Message); cmd.ExecuteNonQuery(); con.Close(); } catch(exception ex) { } }
then run this program in debug mode then you will get exact error message that will help you to solve this issue by yourself.
这篇关于值不在数据库中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文