DateGrid怎样连接数据库(请详细一点)菜鸟
DateGrid怎样连接数据库(请详细一点)菜鸟 问题点数:0、回复次数:6Top
1 楼panda2fw2(我爱Monkey)回复于 2004-05-01 19:18:40 得分 0
简单一点的,你把数据库里的数据读出来,绑定到datagrid。Top
2 楼flyingdream123()回复于 2004-05-01 19:33:54 得分 0
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;
using System.Data.Common;
namespace WindowsApplication7
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.DataGrid dataGrid1;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Button button3;
private System.Windows.Forms.Button button4;
private System.Windows.Forms.Button button5;
private System.Windows.Forms.Button button6;
private System.Data.DataSet ds;
private System.Windows.Forms.BindingManagerBase dbm;
/// <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 Form Designer generated code
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.dataGrid1 = new System.Windows.Forms.DataGrid();
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.button3 = new System.Windows.Forms.Button();
this.button4 = new System.Windows.Forms.Button();
this.button5 = new System.Windows.Forms.Button();
this.button6 = new System.Windows.Forms.Button();
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();
this.SuspendLayout();
//
// dataGrid1
//
this.dataGrid1.AlternatingBackColor = System.Drawing.Color.GhostWhite;
this.dataGrid1.BackColor = System.Drawing.Color.GhostWhite;
this.dataGrid1.BackgroundColor = System.Drawing.Color.Lavender;
this.dataGrid1.CaptionBackColor = System.Drawing.Color.RoyalBlue;
this.dataGrid1.CaptionForeColor = System.Drawing.Color.White;
this.dataGrid1.DataMember = "";
this.dataGrid1.Dock = System.Windows.Forms.DockStyle.Top;
this.dataGrid1.FlatMode = true;
this.dataGrid1.Font = new System.Drawing.Font("Tahoma", 8F);
this.dataGrid1.ForeColor = System.Drawing.Color.MidnightBlue;
this.dataGrid1.GridLineColor = System.Drawing.Color.RoyalBlue;
this.dataGrid1.HeaderBackColor = System.Drawing.Color.MidnightBlue;
this.dataGrid1.HeaderFont = new System.Drawing.Font("Tahoma", 8F, System.Drawing.FontStyle.Bold);
this.dataGrid1.HeaderForeColor = System.Drawing.Color.Lavender;
this.dataGrid1.LinkColor = System.Drawing.Color.Teal;
this.dataGrid1.Name = "dataGrid1";
this.dataGrid1.ParentRowsBackColor = System.Drawing.Color.Lavender;
this.dataGrid1.ParentRowsForeColor = System.Drawing.Color.MidnightBlue;
this.dataGrid1.SelectionBackColor = System.Drawing.Color.Teal;
this.dataGrid1.SelectionForeColor = System.Drawing.Color.PaleGreen;
this.dataGrid1.Size = new System.Drawing.Size(448, 176);
this.dataGrid1.TabIndex = 0;
//
// button1
//
this.button1.Location = new System.Drawing.Point(80, 192);
this.button1.Name = "button1";
this.button1.TabIndex = 1;
this.button1.Text = "loaddata";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(336, 192);
this.button2.Name = "button2";
this.button2.TabIndex = 2;
this.button2.Text = "button2";
//
// button3
//
this.button3.Location = new System.Drawing.Point(16, 240);
this.button3.Name = "button3";
this.button3.TabIndex = 3;
this.button3.Text = "first";
this.button3.Click += new System.EventHandler(this.button3_Click);
//
// button4
//
this.button4.Location = new System.Drawing.Point(104, 240);
this.button4.Name = "button4";
this.button4.TabIndex = 4;
this.button4.Text = "next";
this.button4.Click += new System.EventHandler(this.button4_Click);
//
// button5
//
this.button5.Location = new System.Drawing.Point(200, 240);
this.button5.Name = "button5";
this.button5.TabIndex = 5;
this.button5.Text = "pri";
this.button5.Click += new System.EventHandler(this.button5_Click);
//
// button6
//
this.button6.Location = new System.Drawing.Point(296, 240);
this.button6.Name = "button6";
this.button6.TabIndex = 6;
this.button6.Text = "end";
this.button6.Click += new System.EventHandler(this.button6_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(448, 294);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.button6,
this.button5,
this.button4,
this.button3,
this.button2,
this.button1,
this.dataGrid1});
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 loaddata()
{
string connstr,sqlstr;
connstr="data source=(local);initial catalog=pubs;persist security info=False;user id=sa;workstation id=CJ;packet size=4096";
sqlstr="select * from authors ";
SqlConnection conn=new SqlConnection(connstr);
// conn.Open;
SqlDataAdapter ad=new SqlDataAdapter(sqlstr,connstr);
ds=new DataSet();
ad.Fill(ds,"authors");
dataGrid1.DataSource=ds;
dataGrid1.DataMember=ds.Tables["authors"].ToString();
dbm= this.BindingContext[ds,"authors"];
}
private void button1_Click(object sender, System.EventArgs e)
{
loaddata();
// dataGrid1.DataBindings();
}
private void button3_Click(object sender, System.EventArgs e)
{
// this.BindingContext[ds,"authors"].Position=0;
dbm.Position=0;
}
private void button4_Click(object sender, System.EventArgs e)
{
if (this.BindingContext[ds,"authors"].Position<this.BindingContext[ds,"authors"].Count )
{
// this.BindingContext[ds,"authors"].Position+=1;
dbm.Position+=1;
}
}
private void button5_Click(object sender, System.EventArgs e)
{
if(this.BindingContext[ds,"authors"].Position>0)
dbm.Position-=1;
// this.BindingContext[ds,"authors"].Position-=1;
}
private void button6_Click(object sender, System.EventArgs e)
{
// this.BindingContext[ds,"authors"].Position=this.BindingContext[ds,"authors"].Count-1;
dbm.Position=dbm.Count-1;
}
}
}
Top
3 楼kingapex1(忘掉将来)回复于 2004-05-01 19:35:32 得分 0
用数据建立 datatable 然后datatable邦定datagridTop
4 楼yanhang00(DiM)回复于 2004-05-01 20:04:48 得分 0
ms-help://MS.VSCC.2003/MS.MSDNQTR.2003FEB.2052/vsintro7/html/vxwlkwalkthroughcreatingwebapplicationusingvisualcorvisualbasic.htm
Top
5 楼lzh3692003(无心睡眠)回复于 2004-08-13 18:20:31 得分 0
public void ExecuteQueryAdminGroup( System.Windows.Forms.DataGrid pDataGrid)
{
SqlCommand cmd = new SqlCommand(); // 执行SQL语句
string sql = " SELECT * FROM ADMIN_GROUP "; // SQL 语句
try
{
// 没连接上数据库时返回
if ( !isConnected() )
return;
cmd.Connection = m_sqlConn; // 连接数据库
cmd.CommandText = sql; // 查询的SQL语句
cmd.CommandType = CommandType.Text;
// 创建一个数据库查询适配器
SqlDataAdapter adp = new SqlDataAdapter();
adp.SelectCommand = cmd;
adp.Fill(ds,this.GetDBName());
pDataGrid.DataSource = ds.Tables[this.GetDBName()];
return;
}
catch( Exception ex )
{
MessageBox.Show( ex.Message );
MessageBox.Show( ex.ToString() );
// 判断数据记录中缓冲不为空时清除
if ( ds != null )
ds.Clear();
}
}
以上是个函数,然后自己调用它出来Top
6 楼pll37(海天相接)回复于 2004-08-13 20:09:09 得分 0
using System;
using System.Collections;
using System.Data;
using System.Data.SqlClient; //必须进行引用,对于sqlServer数据库
using System.Web.UI;
private void Connection()
{
SqlConnection conn = new SqlConnection( "data source=数据库服务器名;initial catalog=数据库名;User Id=sa;Password=数据库密码;" );
}
就可以连上数据库了
Top




