c#新增一条数据记录代码怎么写

来源:百度知道 编辑:UC知道 时间:2024/05/10 04:53:25
我用的是.Net2003,SQL server
请知道的写下代码谢谢

string strSqlCommand = string.Format("Insert into Users(UserID,Name,Age) values ('{0}','{1}','{2}')",Guid.NewGuid().ToString(),"张三","27")
SqlConnection v_SqlConnection = new SqlConnection("server=Jebel;database=TestDB;uid=sa;pwd=sa");
v_SqlConnection.Open();
SqlCommand v_SqlCommand = new SqlCommand(strSqlCommand,v_SqlConnection);

try
{
v_SqlCommand.ExecuteNonQuery();

}
catch(SqlException se)
{
MessageBox.Show(se.Message);
}
finally
{
v_SqlConnection.Close();
}

insert into 表名 (字段1,字段2,.....) values (值1,值2,......)