vs.net2005 将数据插入数据库 的函数怎么写,??急

来源:百度知道 编辑:UC知道 时间:2024/05/31 08:07:01

private bool InsertRecord(string id, string name, string sex)
{
string sqlString = "Insert into tablename(Id,Name,Sex) values('{0}','{1}','{2}')";
sqlString = string.Format(sqlString,id,name,sex);
int EffectCount = 0;

SqlCommand command = new SqlCommand(sqlString, new SqlConnection("server=.;database=databasename;uid=sa;psd=sa;"));
command.CommandType = CommandType.Text;

try
{
command.Connection.Open();
EffectCount = command.ExecuteNonQuery();
}
catch (Exception err)
{
MessageBox.Show(err.Message);
}
finally
{
command.Connection.Close();
}

return (EffectCount>0);