如何在C#方法中写查询语句
5个回答
展开全部
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Collections;
namespace MyWindows
{
public partial class Form1 : Form
{
private SqlConnection conn;
private SqlCommand cmd;
private const string connStr = "server=.;uid=sa;pwd=1234;database=数据库名";
public Form1()
{
InitializeComponent();
GetData();
}
//这个方法就是查询语句方法
private void GetData()
{
DataTable dt = GetTable("select * from 表名");
}
#region 数据库操作
/// <summary>
/// 打开链接
/// </summary>
/// <returns></returns>
private SqlConnection GetConn()
{
if (conn == null) conn = new SqlConnection(connStr);
if (conn.State == ConnectionState.Closed) conn.Open();
else if (conn.State == ConnectionState.Broken)
{
conn.Close();
conn.Open();
}
return conn;
}
/// <summary>
/// 执行增删改查操作
/// </summary>
/// <param name="sql"></param>
/// <returns></returns>
private int ExecuteNonQuery(string sql)
{
try
{
cmd = new SqlCommand(sql, GetConn());
return cmd.ExecuteNonQuery();
}
catch
{
return 0;
}
finally
{
conn.Close();
}
}
/// <summary>
/// 读数据
/// </summary>
/// <param name="sql"></param>
/// <returns></returns>
private SqlDataReader ExecuteReader(string sql)
{
try
{
cmd = new SqlCommand(sql, GetConn());
return cmd.ExecuteReader();
}
catch
{
return null;
}
finally
{
conn.Close();
}
}
/// <summary>
/// 得到该表数据
/// </summary>
/// <param name="sql"></param>
/// <returns></returns>
private DataTable GetTable(string sql)
{
try
{
SqlDataAdapter da = new SqlDataAdapter(sql, GetConn());
DataSet ds = new DataSet();
da.Fill(ds);
return ds.Tables[0];
}
catch
{
return null;
}
finally
{
conn.Close();
}
}
#endregion
}
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Collections;
namespace MyWindows
{
public partial class Form1 : Form
{
private SqlConnection conn;
private SqlCommand cmd;
private const string connStr = "server=.;uid=sa;pwd=1234;database=数据库名";
public Form1()
{
InitializeComponent();
GetData();
}
//这个方法就是查询语句方法
private void GetData()
{
DataTable dt = GetTable("select * from 表名");
}
#region 数据库操作
/// <summary>
/// 打开链接
/// </summary>
/// <returns></returns>
private SqlConnection GetConn()
{
if (conn == null) conn = new SqlConnection(connStr);
if (conn.State == ConnectionState.Closed) conn.Open();
else if (conn.State == ConnectionState.Broken)
{
conn.Close();
conn.Open();
}
return conn;
}
/// <summary>
/// 执行增删改查操作
/// </summary>
/// <param name="sql"></param>
/// <returns></returns>
private int ExecuteNonQuery(string sql)
{
try
{
cmd = new SqlCommand(sql, GetConn());
return cmd.ExecuteNonQuery();
}
catch
{
return 0;
}
finally
{
conn.Close();
}
}
/// <summary>
/// 读数据
/// </summary>
/// <param name="sql"></param>
/// <returns></returns>
private SqlDataReader ExecuteReader(string sql)
{
try
{
cmd = new SqlCommand(sql, GetConn());
return cmd.ExecuteReader();
}
catch
{
return null;
}
finally
{
conn.Close();
}
}
/// <summary>
/// 得到该表数据
/// </summary>
/// <param name="sql"></param>
/// <returns></returns>
private DataTable GetTable(string sql)
{
try
{
SqlDataAdapter da = new SqlDataAdapter(sql, GetConn());
DataSet ds = new DataSet();
da.Fill(ds);
return ds.Tables[0];
}
catch
{
return null;
}
finally
{
conn.Close();
}
}
#endregion
}
}
展开全部
///a,b,c,d为四个文本框,s为精确查询还是模糊查询
public datatable search(string a,string b,string c,string d,bool s)
{
StringBulider str =new StringBulider();
str.Append("select * from 表 where zt = 1"); ///只显示非保密的数据
if(!string.IsNullOrEmpty(a))
{
str.Append(sqlstr(a,[a字段名],s));
}
if(!string.IsNullOrEmpty(b))
{
str.Append(sqlstr(b,[b字段名],s));
}
if(!string.IsNullOrEmpty(c))
{
str.Append(sqlstr(c,[c字段名],s));
}
if(!string.IsNullOrEmpty(d))
{
str.Append(sqlstr(d,[d字段名],s));
}
///str就是需要执行的sql语句,查询自己去写
}
------------------------------------------------------------
public string sqlstr(string val,string tix,bool search)
{
string str = "";
if(val != null && val != "")
{
if(search)
{
str =" and" + tix+" like '%"+val+"%'";
}
else
{
str = " and" + tix +" = " +val;
}
}
return str;
}
public datatable search(string a,string b,string c,string d,bool s)
{
StringBulider str =new StringBulider();
str.Append("select * from 表 where zt = 1"); ///只显示非保密的数据
if(!string.IsNullOrEmpty(a))
{
str.Append(sqlstr(a,[a字段名],s));
}
if(!string.IsNullOrEmpty(b))
{
str.Append(sqlstr(b,[b字段名],s));
}
if(!string.IsNullOrEmpty(c))
{
str.Append(sqlstr(c,[c字段名],s));
}
if(!string.IsNullOrEmpty(d))
{
str.Append(sqlstr(d,[d字段名],s));
}
///str就是需要执行的sql语句,查询自己去写
}
------------------------------------------------------------
public string sqlstr(string val,string tix,bool search)
{
string str = "";
if(val != null && val != "")
{
if(search)
{
str =" and" + tix+" like '%"+val+"%'";
}
else
{
str = " and" + tix +" = " +val;
}
}
return str;
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2011-07-04
展开全部
C#中查询语句一般是字符串,也就是说你在SQL中如何写的,在C#中一样的写,只是这时的SQL语言是个字符串而已
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2011-07-04
展开全部
书上找找,会找到的
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
建议楼主看书去!
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询