8 个解决方案
#1
public static void Show()
{
SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=ST_StuManager;Integrated Security=True");
SqlDataAdapter sda = new SqlDataAdapter("select * from ST_student", con);
DataSet ds=new DataSet();
sda.Fill(ds,"stuTable");
foreach (DataRow item in ds.Tables["stuTable"].Rows)
{
Console.WriteLine(item[0]+"\t"+item[1]);
}
}
static void Main()
{
Show();
}
是不是这样啊
{
SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=ST_StuManager;Integrated Security=True");
SqlDataAdapter sda = new SqlDataAdapter("select * from ST_student", con);
DataSet ds=new DataSet();
sda.Fill(ds,"stuTable");
foreach (DataRow item in ds.Tables["stuTable"].Rows)
{
Console.WriteLine(item[0]+"\t"+item[1]);
}
}
static void Main()
{
Show();
}
是不是这样啊
#2
微软又不是吹出来的。你的这个需求,跟直接使用人家ADO.NET有什么区别?
#3
/// <summary>
/// 返回一个可供操作DataTable,支持Sql和OleDB
/// </summary>
/// <param name="strSQL">SQL语句</param>
/// <param name="Connection">一个连接对象(OleDb或Sql类型)</param>
/// <returns></returns>
public static DataTable GetDataTable(string strSQL,IDbConnection Connection)
{
DataTable dt = new DataTable("BT");
DbDataAdapter da = null;
if(Connection is SqlConnection)
{
da = new SqlDataAdapter(strSQL, Connection as SqlConnection);
}
else
if(Connection is OleDbConnection)
{
da = new OleDbDataAdapter(strSQL, Connection as OleDbConnection);
}
else
{
throw new Exception("请传递一个OleDb或Sql类型的连接对象!");
}
da.Fill(dt);
return dt;
}
#4
我才接触api好复杂
#5
sqlhelper.cs
http://blog.jdedu.net/UploadFiles/2007-2/24915621.rar
http://blog.jdedu.net/UploadFiles/2007-2/24915621.rar
#6
/// <summary>
/// 执行SQL语句,返回影响的记录数
/// </summary>
/// <param name="SQLString">SQL语句</param>
/// <returns>影响的记录数</returns>
public static int ExecuteSql(string SQLString)
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
using (SqlCommand cmd = new SqlCommand(SQLString, connection))
{
try
{
connection.Open();
int rows = cmd.ExecuteNonQuery();
return rows;
}
catch (System.Data.SqlClient.SqlException e)
{
connection.Close();
throw e;
}
}
}
}
都是静态函数,放在抽象类里面,要用直接调用就行
/// 执行SQL语句,返回影响的记录数
/// </summary>
/// <param name="SQLString">SQL语句</param>
/// <returns>影响的记录数</returns>
public static int ExecuteSql(string SQLString)
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
using (SqlCommand cmd = new SqlCommand(SQLString, connection))
{
try
{
connection.Open();
int rows = cmd.ExecuteNonQuery();
return rows;
}
catch (System.Data.SqlClient.SqlException e)
{
connection.Close();
throw e;
}
}
}
}
/// <summary>
/// 执行查询语句,返回DataSet
/// </summary>
/// <param name="SQLString">查询语句</param>
/// <returns>DataSet</returns>
public static DataSet Query(string SQLString)
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
DataSet ds = new DataSet();
try
{
connection.Open();
SqlDataAdapter command = new SqlDataAdapter(SQLString, connection);
command.Fill(ds, "ds");
}
catch (System.Data.SqlClient.SqlException ex)
{
throw new Exception(ex.Message);
}
return ds;
}
}
都是静态函数,放在抽象类里面,要用直接调用就行
#7
谢谢啦!
#8
实例化类对象,你写的公共函数中应该有一个SQL语句的参数吧。
利用对象调用方法,并把查询SQL语句传入即可。
利用对象调用方法,并把查询SQL语句传入即可。
#1
public static void Show()
{
SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=ST_StuManager;Integrated Security=True");
SqlDataAdapter sda = new SqlDataAdapter("select * from ST_student", con);
DataSet ds=new DataSet();
sda.Fill(ds,"stuTable");
foreach (DataRow item in ds.Tables["stuTable"].Rows)
{
Console.WriteLine(item[0]+"\t"+item[1]);
}
}
static void Main()
{
Show();
}
是不是这样啊
{
SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=ST_StuManager;Integrated Security=True");
SqlDataAdapter sda = new SqlDataAdapter("select * from ST_student", con);
DataSet ds=new DataSet();
sda.Fill(ds,"stuTable");
foreach (DataRow item in ds.Tables["stuTable"].Rows)
{
Console.WriteLine(item[0]+"\t"+item[1]);
}
}
static void Main()
{
Show();
}
是不是这样啊
#2
微软又不是吹出来的。你的这个需求,跟直接使用人家ADO.NET有什么区别?
#3
/// <summary>
/// 返回一个可供操作DataTable,支持Sql和OleDB
/// </summary>
/// <param name="strSQL">SQL语句</param>
/// <param name="Connection">一个连接对象(OleDb或Sql类型)</param>
/// <returns></returns>
public static DataTable GetDataTable(string strSQL,IDbConnection Connection)
{
DataTable dt = new DataTable("BT");
DbDataAdapter da = null;
if(Connection is SqlConnection)
{
da = new SqlDataAdapter(strSQL, Connection as SqlConnection);
}
else
if(Connection is OleDbConnection)
{
da = new OleDbDataAdapter(strSQL, Connection as OleDbConnection);
}
else
{
throw new Exception("请传递一个OleDb或Sql类型的连接对象!");
}
da.Fill(dt);
return dt;
}
#4
我才接触api好复杂
#5
sqlhelper.cs
http://blog.jdedu.net/UploadFiles/2007-2/24915621.rar
http://blog.jdedu.net/UploadFiles/2007-2/24915621.rar
#6
/// <summary>
/// 执行SQL语句,返回影响的记录数
/// </summary>
/// <param name="SQLString">SQL语句</param>
/// <returns>影响的记录数</returns>
public static int ExecuteSql(string SQLString)
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
using (SqlCommand cmd = new SqlCommand(SQLString, connection))
{
try
{
connection.Open();
int rows = cmd.ExecuteNonQuery();
return rows;
}
catch (System.Data.SqlClient.SqlException e)
{
connection.Close();
throw e;
}
}
}
}
都是静态函数,放在抽象类里面,要用直接调用就行
/// 执行SQL语句,返回影响的记录数
/// </summary>
/// <param name="SQLString">SQL语句</param>
/// <returns>影响的记录数</returns>
public static int ExecuteSql(string SQLString)
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
using (SqlCommand cmd = new SqlCommand(SQLString, connection))
{
try
{
connection.Open();
int rows = cmd.ExecuteNonQuery();
return rows;
}
catch (System.Data.SqlClient.SqlException e)
{
connection.Close();
throw e;
}
}
}
}
/// <summary>
/// 执行查询语句,返回DataSet
/// </summary>
/// <param name="SQLString">查询语句</param>
/// <returns>DataSet</returns>
public static DataSet Query(string SQLString)
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
DataSet ds = new DataSet();
try
{
connection.Open();
SqlDataAdapter command = new SqlDataAdapter(SQLString, connection);
command.Fill(ds, "ds");
}
catch (System.Data.SqlClient.SqlException ex)
{
throw new Exception(ex.Message);
}
return ds;
}
}
都是静态函数,放在抽象类里面,要用直接调用就行
#7
谢谢啦!
#8
实例化类对象,你写的公共函数中应该有一个SQL语句的参数吧。
利用对象调用方法,并把查询SQL语句传入即可。
利用对象调用方法,并把查询SQL语句传入即可。