if (cnn.ConnectionString=="") cnn.ConnectionString=conStr; if (cnn.State!=ConnectionState.Open) cnn.Open(); cmd.Connection=cnn; cmd.CommandType=CommandType.Text; cmd.CommandText="select * from "+tblName.TableName+" where "+sqlStr; da.SelectCommand=cmd;
SqlConnection conn = new SqlConnection("Server=(local);uid=sa;pwd=;database=pubs");
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandType = CommandTpye.Text;
cmd.CommandText = sql语句;
cmd.Connection.Open();
adapter.SqlCommand=cmd;
DataSet ds=new DataSet();
adapter.fill(ds)前台grid
this.datagrid1.datasource=ds.Tables[0]
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;namespace Test
{
/// <summary>
///
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.ColumnHeader columnHeader1;
private System.Windows.Forms.ColumnHeader columnHeader2;
private System.Windows.Forms.ColumnHeader columnHeader3;
private System.Windows.Forms.DataGrid dataGrid1; public Form1()
{
//
//
//
InitializeComponent();
//
// T
//
} /// <summary>
///
/// </summary>
protected override void Dispose( bool disposing )
{
base.Dispose( disposing );
} #region Windows
/// <summary>
///
/// </summary>
private void InitializeComponent()
{
this.columnHeader1 = new System.Windows.Forms.ColumnHeader();
this.columnHeader2 = new System.Windows.Forms.ColumnHeader();
this.columnHeader3 = new System.Windows.Forms.ColumnHeader();
this.dataGrid1 = new System.Windows.Forms.DataGrid();
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();
this.SuspendLayout();
//
// columnHeader1
//
this.columnHeader1.Width = 100;
//
// columnHeader2
//
this.columnHeader2.Width = 150;
//
// columnHeader3
//
this.columnHeader3.Width = 300;
//
// dataGrid1
//
this.dataGrid1.DataMember = "";
this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
this.dataGrid1.Location = new System.Drawing.Point(104, 104);
this.dataGrid1.Name = "dataGrid1";
this.dataGrid1.Size = new System.Drawing.Size(432, 232);
this.dataGrid1.TabIndex = 0;
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 12);
this.ClientSize = new System.Drawing.Size(824, 430);
this.Controls.Add(this.dataGrid1);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
this.ResumeLayout(false); }
#endregion /// <summary>
///
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
} DataTable dt;
private void Form1_Load(object sender, System.EventArgs e)
{
SqlDataAdapter adapter=new SqlDataAdapter();
SqlConnection conn = new SqlConnection("Server=(local);uid=sa;pwd=;database=pubs");
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandType = CommandTpye.Text;
cmd.CommandText = "select col1 from table1";
cmd.Connection.Open();
adapter.SqlCommand=cmd;
DataSet ds=new DataSet();
adapter.fill(ds); dt = ds.Tables[0];
this.dataGrid1.DataSource = dt;
}
}
}
#region fillTable /// <summary>
/// 填充dataTable的查询
/// </summary>
/// <param name="tblName">数据表(必须输入数据库中存在的名称,也可以是视图)</param>
/// <param name="sqlStr">SQL语句</param>
/// <returns>记录条数</returns>
public int select(DataTable tblName,string sqlStr)
{
int i=0; // try
// {
//
tblName.Clear();
da.Dispose();
if (cnn.ConnectionString=="")
cnn.ConnectionString=conStr;
if (cnn.State!=ConnectionState.Open)
cnn.Open(); cmd.Connection=cnn;
cmd.CommandType=CommandType.Text;
cmd.CommandText="select * from "+tblName.TableName+" where "+sqlStr;
da.SelectCommand=cmd;
i=da.Fill(tblName);
//
//
// }
// catch(Exception ce)
// {
// Console.WriteLine("产生错误:\n{0}",ce.Message);
// }
// finally
// {
//this.da.Dispose();
cnn.Close();
// }
return i;
}