public insert() {
Console.Write("请输入姓名:");
String name = Console.ReadLine();
Console.Write("请输入年龄:");
String age = Console.ReadLine();
Console.Write("请输入地址:");
String add = Console.ReadLine();
SqlConnection conn = new SqlConnection(@"server=.;database=mydb;uid=sa;pwd=sa");
//第一种占位符的使用方式
SqlCommand cmd = conn.CreateCommand();
cmd.CommandText = "insert into login values(@username,@userage,@useradd)";
cmd.Parameters.AddWithValue("@username", name);
cmd.Parameters.AddWithValue("@userage", age);
cmd.Parameters.AddWithValue("@useradd", add);
//第二种占位符的使用方式
String sql = string.Format("insert into login values('{0}',{1},'{2}')", name, age, add);
SqlCommand cmd = new SqlCommand(sql, conn);
try {
conn.Open();
int i = cmd.ExecuteNonQuery();
if (i > 0) {
Console.WriteLine("插入数据成功");
}
cmd.Dispose();
conn.Close();
} finally {
conn.Close();
}
}
售后响应及时
7×24小时客服热线数据备份
更安全、更高效、更稳定价格公道精准
项目经理精准报价不弄虚作假合作无风险
重合同讲信誉,无效全额退款