class Program
{
static void Main(string[] args)
{
//数据库的增删改查
string MySqlStr = "Database=vr88;server=localhost;User Id=root;Password=;";
MySqlConnection mySqlcon = new MySqlConnection(MySqlStr);//连接数据库;
mySqlcon.Open();
//查
string str = "select * from student where sid=3;";
MySqlCommand mySqlCom = new MySqlCommand(str, mySqlcon);
CheckedMySql(mySqlCom);
//增
str = "insert into student values(15,'神10',28,'女');";
mySqlCom = new MySqlCommand(str, mySqlcon);
IducMySql(mySqlCom);
//删
str = "delete from student where sid=5;";
mySqlCom = new MySqlCommand(str, mySqlcon);
IducMySql(mySqlCom);
// 改
str = "update student set sage=33 where sid=9;";
mySqlCom = new MySqlCommand(str, mySqlcon);
IducMySql(mySqlCom);
GetId(9);
Console.ReadKey();
}
//(查)
static private void CheckedMySql(MySqlCommand mySqlcom)
{
MySqlDataReader read = mySqlcom.ExecuteReader();
try
{
while (read.Read())
{
if (read.HasRows)//查找
{
Console.WriteLine("ID:{0} 姓名:{1} 年龄:{2} 性别:{3}", read.GetInt32(0), read.GetString(1), read.GetInt32(2), read.GetString(3));
}
}
}
catch (Exception e)
{
throw e;
}
finally
{
read.Close();
}
}
//增 删 改 查
static private void IducMySql(MySqlCommand mySqlcom)
{
try
{
mySqlcom.ExecuteNonQuery();
}
catch (Exception e)
{
throw e;
}
}
//指定ID参数打印相关信息
static private void GetId(int id)
{
string MySqlStr = "Database=vr88;server=localhost;User Id=root;Password=;";
MySqlConnection mySqlcon = new MySqlConnection(MySqlStr);
mySqlcon.Open();
string str = "select * from student where sid=" + id;//注意 别把分号打上去;
MySqlCommand mySqlCom = new MySqlCommand(str, mySqlcon);
MySqlDataReader reader = mySqlCom.ExecuteReader();
try
{
while (reader.Read())
{
if (reader.HasRows)//查找
{
Console.WriteLine("ID:{0} 姓名:{1} 年龄:{2} 性别:{3}", reader.GetInt32(0), reader.GetString(1), reader.GetInt32(2), reader.GetString(3));
}
}
}
catch (Exception e)
{
throw e;
}
finally
{
reader.Close();
}
}
}
Unity3D开发-C#语言进阶篇(服务器常用方法之增删改查)
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...