CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
可用分押宝游戏火热进行中... 专题改版:Java Web 专题
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  .NET技术 >  C#

DateGrid怎样连接数据库(请详细一点)菜鸟

楼主free022()2004-05-01 19:07:46 在 .NET技术 / C# 提问

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

相关问题

  • 数据库菜鸟问题
  • 菜鸟问题(数据库)
  • 数据库菜鸟问题
  • 菜鸟求 C++ Builder 调用 mysql 数据库的详细方法,分不够只管说!!!
  • 关于数据库的菜鸟问题
  • 菜鸟问题,关于数据库
  • 连接数据库问题-菜鸟级
  • 菜鸟问题:数据库连接
  • 菜鸟的数据库发布问题
  • 一个数据库菜鸟问题

关键词

  • datagrid
  • 数据库
  • ds
  • 连接
  • 语句
  • button
  • drawing
  • bindingcontext
  • tabindex
  • lavender

得分解答快速导航

  • 帖主:free022

相关链接

  • CSDN .NET频道
  • .NET类图书
  • C#类图书
  • .NET类源码下载

广告也精彩

反馈

请通过下述方式给我们反馈
反馈
提问
网站简介|广告服务|VIP资费标准|银行汇款帐号|网站地图|帮助|联系方式|诚聘英才|English|问题报告
世纪乐知(北京)网络技术有限公司 版权所有, 京 ICP 证 020026 号
北京创新乐知广告有限公司 提供技术支持
Copyright © 2000-2007, CSDN.NET, All Rights Reserved
GongshangLogo