C#新手提问:怎样连接ACCESS数据库? (20分)

  • 主题发起人 wangxian11
  • 开始时间
W

wangxian11

Unregistered / Unconfirmed
GUEST, unregistred user!
还有怎样连接另外一台电脑上的SQL SERVER?
 
拜托谁能帮我解决。我想学过C#的人应该能解决的吧
 
C#中不是有控件吗,连接ACCESS
using System.Data;
using System.Data.OleDb ;
OleDbConnection con=new OleDbConnection() ;
con.ConnectionString="Provider=Microsoft.jet.oledb.4.0;DataSource=你的文件名";
con.Open() ;
SQL:
using System.Data;
using System.Data.SqlClient ;
SqlConnection con=new SqlConnection() ;
con.ConnectionString="server=机器名;database=数据库名;uid=sa;pwd=''" ;
con.open();
这样应该就可以了,建议找本ADO.NET的书看看可能会更详细一些
 
我的问题是连接上去了,字段也出来了,开始在
private void button1_Click(object sender, System.EventArgs e)
{
oleDbDataAdapter1.Fill(myDataSet11.Table1);
}
照着书上写,怎么就不能看到数据呢?
 
界面上用的什么显示控件?DataGrid? 设置一下不就行了。
 
我用的是DataGrid,字段名都出来,数据出不来,在那里设置呢?
 
DataGrid.DataSource = myDataSet11.Table1;
试试
 
我这里是这么填的,字段名都出来了,可是数据没有出来啊。。
什么原因呢?
 
1。你的Table1的记录吗?
2.重新检查一下数据链:connection -->adapter --> dataSet
 
不行,气死我了,这么简单的都搞不定,和DELPHI到底是不一样
你能给我一个例子吗?连接好的例子。
 
DataGrid.DataSource = myDataSet11;
DataGrid.DataMenber ="数据库表名";
加上这两句就可以显示出来了
 
全贴出来了,其实只要按照顺序一个个地设置就没问题。 ^_*
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.OleDb;
namespace DataTemp
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Data.OleDb.OleDbConnection oleDbConnection1;
private System.Data.OleDb.OleDbCommand oleDbCommand1;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.DataGrid dataGrid1;
private System.Data.OleDb.OleDbCommand oleDbSelectCommand1;
private System.Data.OleDb.OleDbCommand oleDbInsertCommand1;
private System.Data.OleDb.OleDbCommand oleDbUpdateCommand1;
private System.Data.OleDb.OleDbCommand oleDbDeleteCommand1;
private System.Data.OleDb.OleDbConnection connAccess;
private DataTemp.accessDataSet accessDataSet1;
private System.Data.OleDb.OleDbDataAdapter odaAccess;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
reset();
}
/// <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.oleDbConnection1 = new System.Data.OleDb.OleDbConnection();
this.oleDbCommand1 = new System.Data.OleDb.OleDbCommand();
this.textBox1 = new System.Windows.Forms.TextBox();
this.button1 = new System.Windows.Forms.Button();
this.dataGrid1 = new System.Windows.Forms.DataGrid();
this.accessDataSet1 = new DataTemp.accessDataSet();
this.oleDbSelectCommand1 = new System.Data.OleDb.OleDbCommand();
this.connAccess = new System.Data.OleDb.OleDbConnection();
this.oleDbInsertCommand1 = new System.Data.OleDb.OleDbCommand();
this.oleDbUpdateCommand1 = new System.Data.OleDb.OleDbCommand();
this.oleDbDeleteCommand1 = new System.Data.OleDb.OleDbCommand();
this.odaAccess = new System.Data.OleDb.OleDbDataAdapter();
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).begin
Init();
((System.ComponentModel.ISupportInitialize)(this.accessDataSet1)).begin
Init();
this.SuspendLayout();
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(464, 320);
this.textBox1.Name = "textBox1";
this.textBox1.TabIndex = 0;
this.textBox1.Text = "textBox1";
//
// button1
//
this.button1.Location = new System.Drawing.Point(104, 320);
this.button1.Name = "button1";
this.button1.TabIndex = 1;
this.button1.Text = "button1";
//
// dataGrid1
//
this.dataGrid1.DataMember = "";
this.dataGrid1.DataSource = this.accessDataSet1._Table;
this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
this.dataGrid1.Location = new System.Drawing.Point(80, 48);
this.dataGrid1.Name = "dataGrid1";
this.dataGrid1.Size = new System.Drawing.Size(384, 192);
this.dataGrid1.TabIndex = 2;
//
// accessDataSet1
//
this.accessDataSet1.DataSetName = "accessDataSet";
this.accessDataSet1.Locale = new System.Globalization.CultureInfo("zh-CN");
//
// oleDbSelectCommand1
//
this.oleDbSelectCommand1.CommandText = "SELECT jz.* FROM jz";
this.oleDbSelectCommand1.Connection = this.connAccess;
//
// connAccess
//
this.connAccess.ConnectionString = @"Jet OLEDB:Global Partial Bulk Ops=2;Jet OLEDB:Registry Path=;Jet OLEDB:Database Locking Mode=1;Jet OLEDB:Database Password=;Data Source=""H:/系统文档/我的文档/Visual Studio Projects/DataTemp/bin/Debug/test.mdb"";Password=;Jet OLEDB:Engine Type=5;Jet OLEDB:Global Bulk Transactions=1;Provider=""Microsoft.Jet.OLEDB.4.0"";Jet OLEDB:System database=;Jet OLEDB:SFP=False;Extended Properties=;Mode=Share Deny None;Jet OLEDB:New Database Password=;Jet OLEDB:Create System Database=False;Jet OLEDB:Don't Copy Locale on Compact=False;Jet OLEDB:Compact Without Replica Repair=False;User ID=Admin;Jet OLEDB:Encrypt Database=False";
//
// odaAccess
//
this.odaAccess.DeleteCommand = this.oleDbDeleteCommand1;
this.odaAccess.InsertCommand = this.oleDbInsertCommand1;
this.odaAccess.SelectCommand = this.oleDbSelectCommand1;
this.odaAccess.UpdateCommand = this.oleDbUpdateCommand1;
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(608, 397);
this.Controls.Add(this.dataGrid1);
this.Controls.Add(this.button1);
this.Controls.Add(this.textBox1);
this.Name = "Form1";
this.Text = "Form1";
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.accessDataSet1)).EndInit();
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void reset ()
{
connAccess.Open();
odaAccess.Fill(accessDataSet1);
connAccess.Close();
}
}
}
 

Similar threads

顶部