帮忙解释一下这段C#代码,详解一下 谢谢

来源:百度知道 编辑:UC知道 时间:2024/07/07 01:02:04
public bool Client_Add(string clientID,string clientName,string sex,string nativePlace)
{
if(this.Client_Search(clientID))
{
return true;
}
this.selectStr="insert into Client values("+"'"+clientID+"',"+"'"+clientName+"','"+sex+"','"+nativePlace+"'"+")";
this.sqlCommand1.CommandText=this.selectStr;
try
{
this.sqlConnection1.Open();
this.sqlCommand1.ExecuteNonQuery();
return true;
}
catch(System.Exception E)
{
Console.WriteLine(E.ToString());
this.sqlConnection1.Close();
return false;
}
finally
{
this.sqlConnection1.Close();
}
}

方法名:Client_Add
返回值:bool
传参: string clientID,string clientName,string sex,string nativePlace
方法内解释:
如果this.Client_Search(clientID) 返回True,就returntrue。
给this.selectStr赋值为一个用参数拼接起来的插入语句
设置this.sqlCommand1的CommandText值为上边的selectStr
先尝试
{
打开数据库连接;
执行sqlCommand1;
成功的话返回true;
}
如果出错了就扑捉异常给异常变量E
{
出错后执行输出E.ToString(),也就是错误信息输出;
关闭数据库连接;
返回false
}
始终执行
{
关闭数据库连接;
}

一段向数据库插入值的代码,把string clientID,string clientName,string sex,string nativePlace 这4个参数的值插入表Client中
如果插入失败,打印错误原因