c#学习ado.net 向数据库插入一个用户名和密码
时间:2011-05-19 来源:smxlcs
namespace 用户名密码插入
{
class Program
{
static void Main(string[] args)
{
string dataDir = AppDomain.CurrentDomain.BaseDirectory;
if (dataDir.EndsWith(@"\bin\Debug\") || dataDir.EndsWith(@"\bin\Release"))
{
dataDir = System.IO.Directory.GetParent(dataDir).Parent.Parent.FullName;
AppDomain.CurrentDomain.SetData("DataDirectory", dataDir);
}
Console.WriteLine("请输入插入的用户名:");
string username = Console.ReadLine();
Console.WriteLine("请输入插入的密码:");
string password = Console.ReadLine();
using (SqlConnection conn = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDBFilename=|DataDirectory|\Database1.mdf;Integrated Security=True;User Instance=True"))
{
conn.Open();
using (SqlCommand cmd = conn.CreateCommand())
{
cmd.CommandText = "Insert into T_Users(UserName,Password) values ('" + username + "','" + password + "')";
cmd.ExecuteNonQuery();
Console.WriteLine("插入成功");
}
}
}
}
}