十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
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();
}
}