请教各位老大,SQL语句在C#下如何执行? 你的意思看得不太明白Select * From mytable Where ..... And .... 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 string connectionString = "server = ; uid = ; psd = ; database = ";string commandString = "Select * from yourtable where... and... ";SqlDataAdapter DataAdapter = new SqlDataAdapter(commandString,connectiongString);DataSet myDataSet = new DataSet();DataAdapter.Fill(DataSet,"yourtable"); 1建立连接 SQLCONNECTION建立SQL命令 SQLCOMMAND建立数据适配器 SqlDataAdapter 执行数据适配器得到数据集。2使用DATAREADER来读出数据。给你一段列程using System;using System.Data.SqlClient;namespace Edoc2002.Public{ /// <summary> /// RspDesc 的摘要说明。 /// </summary> public class RspDesc { public string State=""; public string StateDesc=""; public string DeclNo=""; public string GetDeclNo=""; public string StrRspDesc=""; private string SerialNo=""; public bool isFound=false; private System.Data.SqlClient.SqlDataReader dataReader; public RspDesc(string SeqNo,System.Data.SqlClient.SqlConnection conn) { try { System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand("SELECT DECL_NO,DECL_GET_NO,COMM_FLAG,SERIAL_NO,EDIT_COMM_FLAG FROM SEARCH_EDOC WHERE SEQ_NO='"+SeqNo+"'",conn); dataReader = cmd.ExecuteReader(); if(dataReader.Read()) { //有值,仅有一个 DeclNo=dataReader.GetString(0); GetDeclNo=dataReader.GetString(1); State = dataReader.GetString(2); SerialNo=dataReader.GetSqlDecimal(3).ToString(); StateDesc=dataReader.GetString(4); } else { State="0001"; StrRspDesc="没有回执"; SerialNo=""; } } catch(Exception) { } finally { if(dataReader!=null) { dataReader.Close(); dataReader=null; } } if(!SerialNo.Equals("")) { try { SqlCommand RspCmd = new System.Data.SqlClient.SqlCommand("SELECT DECL_RSP_DESC_RECORD FROM DECL_RESPONSE WHERE SERIAL_NO='"+SerialNo+"'",conn); dataReader=RspCmd.ExecuteReader(); if(dataReader.Read()) { StrRspDesc=dataReader.GetString(0); isFound=true; } } catch(Exception) { } finally { if(dataReader!=null) { dataReader.Close(); dataReader=null; } } } } }} 超级菜的问题C#比较大小。。 异步socket问题:正在接受的套接字不会侦听连接 可用SOCKET虚拟WEB目录吗?不用IIS! 关于截图的问题 在WEB开发中,页面之间变量值如何传递(除了用Session,Cookie、Application) 请推荐一下关于Windows应用程序的设计模式的文章,谢谢。 Winform 中如何添加一个动态的Icon? 菜鸟 求图片放大算法 C#调java webservice接口问题 给定一个日期,如何确定他是星期几呢? 如果今天老板就把你从程序员提升到项目经理,你的压力和感觉会是如何?? C#中有无关于SNMP的类,up者有分。
"server = ; uid = ; psd = ; database = ";
string commandString =
"Select * from yourtable where... and... ";
SqlDataAdapter DataAdapter =
new SqlDataAdapter(commandString,connectiongString);
DataSet myDataSet = new DataSet();
DataAdapter.Fill(DataSet,"yourtable");
建立连接 SQLCONNECTION
建立SQL命令 SQLCOMMAND
建立数据适配器 SqlDataAdapter
执行数据适配器得到数据集。
2
使用DATAREADER来读出数据。
给你一段列程
using System;
using System.Data.SqlClient;
namespace Edoc2002.Public
{
/// <summary>
/// RspDesc 的摘要说明。
/// </summary>
public class RspDesc
{
public string State="";
public string StateDesc="";
public string DeclNo="";
public string GetDeclNo="";
public string StrRspDesc="";
private string SerialNo="";
public bool isFound=false;
private System.Data.SqlClient.SqlDataReader dataReader;
public RspDesc(string SeqNo,System.Data.SqlClient.SqlConnection conn)
{
try
{
System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand("SELECT DECL_NO,DECL_GET_NO,COMM_FLAG,SERIAL_NO,EDIT_COMM_FLAG FROM SEARCH_EDOC WHERE SEQ_NO='"+SeqNo+"'",conn);
dataReader = cmd.ExecuteReader();
if(dataReader.Read())
{
//有值,仅有一个
DeclNo=dataReader.GetString(0);
GetDeclNo=dataReader.GetString(1);
State = dataReader.GetString(2);
SerialNo=dataReader.GetSqlDecimal(3).ToString();
StateDesc=dataReader.GetString(4);
}
else
{
State="0001";
StrRspDesc="没有回执";
SerialNo="";
}
}
catch(Exception)
{
}
finally
{
if(dataReader!=null)
{
dataReader.Close();
dataReader=null;
}
}
if(!SerialNo.Equals(""))
{
try
{
SqlCommand RspCmd = new System.Data.SqlClient.SqlCommand("SELECT DECL_RSP_DESC_RECORD FROM DECL_RESPONSE WHERE SERIAL_NO='"+SerialNo+"'",conn);
dataReader=RspCmd.ExecuteReader();
if(dataReader.Read())
{
StrRspDesc=dataReader.GetString(0);
isFound=true;
}
}
catch(Exception)
{
}
finally
{
if(dataReader!=null)
{
dataReader.Close();
dataReader=null;
}
}
}
}
}
}