关于在vs2005中用c#ado.net与SQL server2005连接的问题 20
我做的错在哪里?它无法把数据写入数据库我已经在web.config中添加了<connectionStrings><addname="DBTestConnectionStr...
我做的错在哪里?
它无法把数据写入数据库
我已经在web.config中添加了
<connectionStrings>
<add name="DBTestConnectionString" connectionString ="Data Source=JZB-PC;Initial Catalog=DBTest;Integrated Security=True"
providerName="System.Data.SqlClient"/>
还是不行
我的代码如下:
protected void Button1_Click(object sender, EventArgs e)
{
if (!IsPostBack)
{
string cs = ConfigurationManager.ConnectionStrings["DBTestConnectionString"].ConnectionString.ToString();
DBTest = new SqlConnection(cs);
DBTest.Open();
string vnosql1 = "select count(*) from volunteer";
string username1 = this.TextBoxUserName.Text;
string vname1 = this.TextBoxName.Text;
string sex1 = "";
if (TextBoxList1.SelectedValue.Trim() == "男")
{
sex1 = "男";
}
else
{
sex1 = "女";
}
string Vpassword1 = this.TextBoxPwd.Text;
string vbirth1 = this.TextBoxBirth.Text;
string vnumber1 = this.TextBoxNumber.Text;
string vaddress1 = this.TextBoxAddress.Text;
//string strSql = "select * from volunteer";
string strSql = "insert into DBTest.dbo.volunteer(vno,username,vname,vsex,Vpassword,vbirth,vnumber,vaddress) values('" + vnosql1 + "','" + username1 + "','" + vname1 + "','" + sex1 + "','" + Vpassword1 + "','" + vbirth1 + "','" + vnumber1 + "','" + vaddress1 + "')";
//OperateDataBase odb = new OperateDataBase();
//bool add = odb.ExceSql(strSql);
SqlCommand dbCmd = new SqlCommand(strSql, DBTest);
try
{
dbCmd.ExecuteNonQuery();
Server.Transfer("注册成功.aspx");
}
catch{
Response.Write("<script language=javascript> alert('添加失败');location='javascript:history.go(-1)'</script>");
}
finally
{
DBTest.Close();
}
}
} 展开
它无法把数据写入数据库
我已经在web.config中添加了
<connectionStrings>
<add name="DBTestConnectionString" connectionString ="Data Source=JZB-PC;Initial Catalog=DBTest;Integrated Security=True"
providerName="System.Data.SqlClient"/>
还是不行
我的代码如下:
protected void Button1_Click(object sender, EventArgs e)
{
if (!IsPostBack)
{
string cs = ConfigurationManager.ConnectionStrings["DBTestConnectionString"].ConnectionString.ToString();
DBTest = new SqlConnection(cs);
DBTest.Open();
string vnosql1 = "select count(*) from volunteer";
string username1 = this.TextBoxUserName.Text;
string vname1 = this.TextBoxName.Text;
string sex1 = "";
if (TextBoxList1.SelectedValue.Trim() == "男")
{
sex1 = "男";
}
else
{
sex1 = "女";
}
string Vpassword1 = this.TextBoxPwd.Text;
string vbirth1 = this.TextBoxBirth.Text;
string vnumber1 = this.TextBoxNumber.Text;
string vaddress1 = this.TextBoxAddress.Text;
//string strSql = "select * from volunteer";
string strSql = "insert into DBTest.dbo.volunteer(vno,username,vname,vsex,Vpassword,vbirth,vnumber,vaddress) values('" + vnosql1 + "','" + username1 + "','" + vname1 + "','" + sex1 + "','" + Vpassword1 + "','" + vbirth1 + "','" + vnumber1 + "','" + vaddress1 + "')";
//OperateDataBase odb = new OperateDataBase();
//bool add = odb.ExceSql(strSql);
SqlCommand dbCmd = new SqlCommand(strSql, DBTest);
try
{
dbCmd.ExecuteNonQuery();
Server.Transfer("注册成功.aspx");
}
catch{
Response.Write("<script language=javascript> alert('添加失败');location='javascript:history.go(-1)'</script>");
}
finally
{
DBTest.Close();
}
}
} 展开
3个回答
展开全部
SqlConnection DBTest = new SqlConnection(ConfigurationManager.ConnectionStrings["DBTestConnectionString"].ToString());
DBTest .Open();
用这个把那两行替换掉试试。不行加断点调试。调试时catch最好是 catch(Exception ex){
throw(ex);
.....
}会抛出错误
DBTest .Open();
用这个把那两行替换掉试试。不行加断点调试。调试时catch最好是 catch(Exception ex){
throw(ex);
.....
}会抛出错误
更多追问追答
追问
我没写后面的catch,把那两行改了之后,他给了个警告说我并未对字段“注册.DBTest”(这个是我写这些代码的网页)赋值,字段将一直保持其默认值null
追答
using System.Data.SqlClient; 加了没有
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
<add key="DBTestConnectionString" value="Server=.;Database=数据库名;uid=登录名;pwd=密码;" />
你试一下这个
你试一下这个
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
哪里有错?什么错误可以贴出来
更多追问追答
追问
没有报错,可是无法将数据写入数据库
追答
你的意思是他执行了: Server.Transfer("注册成功.aspx");但是insert into 却没有执行成功,那这样办:
运行的时候在string strSql = "insert into ……“添加一个断点,将strSql放到SQL Server Manager Studio中执行以下,看看你的SQl语句是否有问题。
如果在SQLServer里面没问题,那就是你的代码有问题了,一般都是细节!
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询