sqlhelper 的使用 (C#)超级详细的入门教程

sqlhelper 的使用 (C#)超级详细的入门教程

sql helper 的使用 (C#)小白教程

提到CRUD,很多刚入门的小白总是来一条写一条连接,先建立连接connection 打开连接 open 来查询query 最后别忘了关闭连接 close 。

要是一次写多个操作,那一遍一遍写下来肯定麻木了。

其实大神们早已写好数据库帮助类来简化这一操作了,使用SQLhelper。

那么如何使用呢?

首先创建sqlhelper类

输入SqlHelper 创建

粘贴这段代码

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Data.SqlClient;

using System.Data;

namespace DCsystem

{

class SqlHelper

{

public string strcon = @"data source=LAPTOP-GT9H3NIK;initial catalog=dc;integrated security=true";

private SqlConnection con = null;

private SqlConnection OpenOrCreateCon()

{

con = new SqlConnection(strcon);

if (con != null && con.State == ConnectionState.Closed)

{ con.Open(); }

return con;

}

private void ClosedCon()

{

if (con != null && con.State == ConnectionState.Open)

{ con.Close(); }

}

public int GetByScalar(string sql)//查询

{

OpenOrCreateCon();

SqlCommand cmd = new SqlCommand(sql, con);

int i = Convert.ToInt32(cmd.ExecuteScalar());

ClosedCon();

return i;

}

public int GetByNonQuery(string sql)//增删改

{

OpenOrCreateCon();

SqlCommand cmd = new SqlCommand(sql, con);

int i = Convert.ToInt32(cmd.ExecuteNonQuery());

ClosedCon();

return i;

}

public int GetByNonQuery(string sql, SqlParameter[] para)

{

OpenOrCreateCon();

SqlCommand cmd = new SqlCommand(sql, con);

cmd.Parameters.AddRange(para);

int i = Convert.ToInt32(cmd.ExecuteNonQuery());

ClosedCon();

return i;

}

public SqlDataReader GetByReader(string sql)

{

OpenOrCreateCon();

SqlCommand cmd = new SqlCommand(sql, con);

SqlDataReader i = cmd.ExecuteReader(CommandBehavior.CloseConnection);

return i;

}

public DataSet GetBySet(string sql)

{

OpenOrCreateCon();

SqlDataAdapter sda = new SqlDataAdapter(sql, con);

DataSet ds = new DataSet();

sda.Fill(ds);

return ds;

}

public DataTable GetByTable(string sql)

{

OpenOrCreateCon();

SqlDataAdapter sda = new SqlDataAdapter(sql, con);

DataTable dt = new DataTable();

sda.Fill(dt);

return dt;

}

public DataView GetByView(string sql)

{

OpenOrCreateCon();

SqlDataAdapter sda = new SqlDataAdapter(sql, con);

DataTable dt = new DataTable();

sda.Fill(dt);

DataView dv = new DataView();

dv.Table = dt;

return dv;

}

}

}

注意namespace 后面的值DCsystem是自己的项目路径,不要写错了。

data source=数据库链接名,initial catalog = 数据库名。

public string strcon = @"data source=LAPTOP-GT9H3NIK;initial catalog=dc;integrated security=true";

然后我们就可以在其他地方使用sqlhelper来节省我们的代码工作量啦!

下面是一个使用案例

在系统的登录页面有一个登录的功能按钮,这里需要查询数据。

在数据库查询代码上就可以写

SqlHelper sqlhelper = new SqlHelper();

String sql = "select count(*) from tb_User where UserName='"+textBox1.Text+"' and UserPwd='"+textBox2.Text+"'";

int i = sqlhelper.GetByScalar(sql);

只需要写sqlhelper.GetByScalar(查询语句) 就可以得到想要的结果,是不是很方便。

private void button1_Click(object sender, EventArgs e)

{

if (textBox1.Text == "")

{

MessageBox.Show("用户名不能为空", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);

}

else

{

if (textBox2.Text == "")

{

MessageBox.Show("密码不能为空", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);

}

else

{

SqlHelper sqlhelper = new SqlHelper();

String sql = "select count(*) from tb_User where UserName='"+textBox1.Text+"' and UserPwd='"+textBox2.Text+"'";

int i = sqlhelper.GetByScalar(sql);

if (i > 0)

{

string sql1 = "select * from tb_User where UserName='"+textBox1.Text+"'";

SqlDataReader sdr = sqlhelper.GetByReader(sql1);

sdr.Read();

if (sdr.HasRows)

{

Form2 f2 = new Form2();

f2.Name = textBox1.Text;

f2.Power = sdr["power"].ToString();

f2.Show();

this.Hide();

}

}

else

{

MessageBox.Show("用户名或密码错误", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);

textBox1.Focus();

textBox1.Text = textBox2.Text = string.Empty;

}

}

}

}

最后也是直接登录成功进入到主页面中。

相关推荐

手机检测软件
office365无法登录账号

手机检测软件

07-23 👁️ 2559
疯狂派对怎么解锁角色 疯狂派对人物解锁方法
office365无法登录账号

疯狂派对怎么解锁角色 疯狂派对人物解锁方法

07-05 👁️ 4307
卡塔尔世界杯
Bet体育365提款流水

卡塔尔世界杯

06-28 👁️ 2557
苹果手机怎么群发短信?这几个方法简单又实用!
office365无法登录账号

苹果手机怎么群发短信?这几个方法简单又实用!

07-01 👁️ 3315