C#链接MySQL数据库的实现步骤有哪些

 我来答
huanglenzhi
2016-12-24 · 知道合伙人数码行家
huanglenzhi
知道合伙人数码行家
采纳数:117540 获赞数:517141
长期从事计算机组装,维护,网络组建及管理。对计算机硬件、操作系统安装、典型网络设备具有详细认知。

向TA提问 私信TA
展开全部
C#连接数据库有以下几个步骤:
1:使用配置的数据库连接串,创建数据库连接 Connection 对象
2:构建操作的sql语句
3:定义command对象
4:打开数据连接
5:执行命令
举一个例子,删除操作

public class StudentService
{
//从配置文件中读取数据库连接字符串
private readonly static string connString = ConfigurationManager.ConnectionStrings["accpConnectionString"].ToString();
private readonly static string dboOwner = ConfigurationManager.ConnectionStrings["DataBaseOwner"].ToString();
AdoNetModels.Student model = new Student();

#region 删除数据1
public int DeleteStudent(int stuID)
{
int result = 0;
// 数据库连接 Connection 对象
SqlConnection connection = new SqlConnection(connString);
// 构建删除的sql语句
string sql = string.Format("Delete From Student Where stuID={0}", stuID);
// 定义command对象
SqlCommand command = new SqlCommand(sql, connection);

try
{
connection.Open();
result = command.ExecuteNonQuery(); // 执行命令
}
catch (Exception ex)
{

Console.WriteLine(ex.Message);
}
finally
{
connection.Close();
}
return result;
}
#endregion
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式