//首先要建立到数据库的连接
SqlConnection sqlConn =new SqlConnection();
sqlConn.ConnectionString="server=(local);uid=sa;pwd=;database=pubs";
sqlConn.Open();
sqlCmd =new SqlCommand("select booksl,bookdj from book",sqlConn);
SqlDataAdapter da=new SqlDataAdapter();
da.SelectCommand=sqlCmd;
DataTable dt =new DataTable();//实例化dt
da.Fill(dt);//填充表格
dataGrid1.DataSource=dt1; //设定dataGrid数据源 你不能显示可能是没有设定数据源或者是没有填充表格 ^_^ 接分 ,呵呵
SqlConnection sqlConn =new SqlConnection();
sqlConn.ConnectionString="server=(local);uid=sa;pwd=;database=pubs";
sqlConn.Open();
sqlCmd =new SqlCommand("select booksl,bookdj from book",sqlConn);
SqlDataAdapter da=new SqlDataAdapter();
da.SelectCommand=sqlCmd;
DataTable dt =new DataTable();//实例化dt
da.Fill(dt);//填充表格
dataGrid1.DataSource=dt1; //设定dataGrid数据源 你不能显示可能是没有设定数据源或者是没有填充表格 ^_^ 接分 ,呵呵
可能是你SQL安装或设置的问题...
少了一句:
dataGrid1.DataBind();
注意一下你的SQL SERVER的验证方式是什么?“仅Windows”验证还是“SQL SERVER和Windows”验证?另外,要赋予相关用户相关的权限。
这里面有所有连接字符串的写法
在WEB中要
asp.net数据库开发入门与提高。是清华的很不错,上手快,而且都是用sqlserver自带的数据库做的实例
example1:using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;namespace Query_and_Display_in_DataGrid
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.TextBox textSQL;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.DataGrid dataGrid1;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null; public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent(); //
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
} /// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
} #region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.label2 = new System.Windows.Forms.Label();
this.label1 = new System.Windows.Forms.Label();
this.textSQL = new System.Windows.Forms.TextBox();
this.button1 = new System.Windows.Forms.Button();
this.dataGrid1 = new System.Windows.Forms.DataGrid();
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();
this.SuspendLayout();
//
// label2
//
this.label2.ForeColor = System.Drawing.Color.Blue;
this.label2.Location = new System.Drawing.Point(8, 168);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(136, 16);
this.label2.TabIndex = 13;
this.label2.Text = "返回结果如下:";
//
// label1
//
this.label1.ForeColor = System.Drawing.Color.Blue;
this.label1.Location = new System.Drawing.Point(8, 8);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(136, 16);
this.label1.TabIndex = 12;
this.label1.Text = "请在下面输入查询指令";
//
// textSQL
//
this.textSQL.Location = new System.Drawing.Point(8, 32);
this.textSQL.Multiline = true;
this.textSQL.Name = "textSQL";
this.textSQL.Size = new System.Drawing.Size(464, 88);
this.textSQL.TabIndex = 11;
this.textSQL.Text = "";
//
// button1
//
this.button1.BackColor = System.Drawing.Color.Silver;
this.button1.Cursor = System.Windows.Forms.Cursors.Hand;
this.button1.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.button1.Font = new System.Drawing.Font("宋体", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.button1.ForeColor = System.Drawing.Color.Blue;
this.button1.Location = new System.Drawing.Point(176, 136);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(120, 40);
this.button1.TabIndex = 10;
this.button1.Tag = "";
this.button1.Text = "开 始 执 行";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// dataGrid1
//
this.dataGrid1.DataMember = "";
this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
this.dataGrid1.Location = new System.Drawing.Point(8, 192);
this.dataGrid1.Name = "dataGrid1";
this.dataGrid1.Size = new System.Drawing.Size(464, 216);
this.dataGrid1.TabIndex = 14;
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(480, 413);
this.Controls.Add(this.dataGrid1);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Controls.Add(this.textSQL);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "Form1";
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
this.ResumeLayout(false); }
#endregion /// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
} private void button1_Click(object sender, System.EventArgs e)
{
SqlConnection thisConnection = new SqlConnection();
SqlCommand cmd = new SqlCommand();
SqlDataAdapter da = new SqlDataAdapter() ;
DataSet mydataSet = new DataSet();
try
{
thisConnection = new SqlConnection(
@"server = CELERON\SQL2000;"+
"integrated Security = sspi;"+
"Database = Northwind");
thisConnection.Open(); if(textSQL.Text!= "")//注意,不是NULL
{
cmd = new SqlCommand(textSQL.Text,thisConnection);
da.SelectCommand =cmd;
} else
{
MessageBox.Show("出现错误,请输入查询语句!","错误");
textSQL.Focus();
return;
} da.Fill(mydataSet); dataGrid1.SetDataBinding(mydataSet,"Table"); MessageBox.Show("hahaha");
thisConnection.Close();
} catch(Exception ex)
{
MessageBox.Show("出现错误:" + ex.Message);
} finally
{
thisConnection.Close();
} }
}
}
2
using System;
using System.Data;
using System.Data.SqlClient;namespace DataReader
{
/// <summary>
/// Class1 的摘要说明。
/// </summary>
class Class1
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main(string[] args)
{
SqlConnection thisConnection = new SqlConnection(
@"server = CELERON\SQL2000;"+
"integrated Security = sspi;"+
"Database = Northwind");
try
{
thisConnection.Open(); string SQL = "SELECT * FROM Customers ;"+
"SELECT * FROM Products WHERE UnitPrice < 20" ; SqlDataAdapter da = new SqlDataAdapter(); da.SelectCommand = new SqlCommand(SQL,thisConnection); DataSet ds = new DataSet(); da.Fill(ds,"Customers");da.Fill(ds,"22"); DataTableCollection dtc = ds.Tables; Console.WriteLine("CUSTOMERS 的结果为:");
Console.WriteLine("CompanyName".PadRight(20)+"\t\t" +
"ContactName".PadRight(20));
Console.WriteLine(); string fl = "Country = 'Germany'"; string sort = "CompanyName ASC"; foreach(DataRow myRow in dtc["Customers"].Select(fl,sort))
{
Console.WriteLine("{0}\t{1}",
myRow["Companyname"].ToString().PadRight(25),
myRow["ContactName"]);
} Console.WriteLine();
Console.WriteLine("==========================");
Console.WriteLine("ProcdctName".PadRight(20)+"\t\t" +
"UnitPrice".PadRight(20));
Console.WriteLine(); foreach(DataRow myRow in dtc["22"].Rows)
{
Console.WriteLine("{0}\t{1}",
myRow["ProductName"].ToString().PadRight(25),
myRow["UnitPrice"]);
} } catch(Exception ex)
{
Console.WriteLine("Error:"+ex.Message);
} finally
{
thisConnection.Close();
Console.ReadLine();
}
}
}
}