各位兄弟朋友:由于刚开始学.net,很多问题都搞不清楚,请各位多多赐教!
    我在web.config中设置了以下:
    <appSettings>
    <add key="strConnection" value="data source=localhost;initial catalog=hotwell;integrated security=SSPI;persist security info=False;user id=sa;workstation id=WANGWD;packet size=4096" />   
   </appSettings>
    想把strConnection作为连接数据库的字符串。
   
    protected static string strConn = ConfigurationSettings.AppSettings["strConnection"];    protected static int ExecuteSql(string strSQL)
{
SqlConnection myCn = new SqlConnection(strConn);
SqlCommand myCmd = new SqlCommand(strSQL,myCn);
try
{
myCn.Open();
myCmd.ExecuteNonQuery();
return 0;
}
catch(System.Data.SqlClient.SqlException e)
{
throw new Exception(e.Message);
}
finally
{
myCmd.Dispose();
myCn.Close();
}
}public  void Addbj(string bjID,string bjmc,string bjsm,string bjbz)
{
if(IfExist(bjID))
{
throw new Exception("此部件已存在!");
}
else
{
strSQL = "Insert into 部件(部件ID,部件名称,部件说明,备注) Values("
+ "'" + bjID + "',"
+ "'" + bjmc + "'"
+ "'" + bjsm + "',"
+ "'" + bjbz + "')" ;
try
{
ExecuteSql(strSQL);
}
catch
{
throw new Exception("添加失败!");
}
}另外:
private void Button4_Click(object sender, System.EventArgs e)
{
try
{
mybujian.Addbj(tb_bjID.Text.Trim(),tb_bjmc.Text.Trim(),tb_bjsm.Text.Trim(),tb_bjbz.Text.Trim());
tb_bjID.Text="";
tb_bjmc.Text ="";
tb_bjsm.Text ="";
tb_bjbz.Text =""; }
catch(Exception er)
{
ShowErrMsg(er.Message);
}
}    
以上是主要的几部分,showerrmsg.()和IfExist()也写了。当调试时,没出错。在出来的页面中输入“部件ID”和“部件名称”等,之后保存,就出现了“添加失败!”了。    请各位有经验的朋友帮忙看看,到底问题最可能出现在哪?小女子不胜感激!