数据库打开都正常,就是没显示数据在datagrid上。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;namespace CustomerEditor
{
public partial class Form1 : Form
{
private OleDbConnection _connection; //添加连接属性
public OleDbConnection Connection
{
get
{
return _connection;
}
set
{
Disconnection();
_connection = value;
} }
//断开数据库连接
public void Disconnection()
{
if (_connection != null)
{
if (_connection.State != ConnectionState.Closed)
_connection.Close();
_connection = null;
} } //意外处理
public void HandleException(string message, Exception EX)
{
MessageBox.Show(this, string.Format("{0}\n{1}:{2}", message, EX.GetType().ToString(), EX.Message)); }
//连接数据库
public void Connect()
{
if (dialogOpenFile.ShowDialog(this) == DialogResult.OK)
{
try
{
string connectionString = string.Format(
"Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};User ID=;Password=;",dialogOpenFile.FileName);
//Create a connection
OleDbConnection newConnection = new OleDbConnection(connectionString);
newConnection.Open();
Connection = newConnection;
}
catch(Exception EX)
{
//
HandleException("无法创建数据连接", EX);
}
} } public Form1()
{
InitializeComponent();
} private void menuDataConnect_Click(object sender, EventArgs e)
{
Connect();
} //装载数据
public void LoadData()
{
//
if (_connection == null)
{
MessageBox.Show(this, "你必须连接一个数据库!");
return;
}
OleDbCommand command = null;
OleDbDataAdapter adapter = null;
try
{
command = _connection.CreateCommand();
command.CommandText = "person";
command.CommandType = CommandType.TableDirect;
adapter = new OleDbDataAdapter(command);
DataSet dataset = new DataSet();
adapter.Fill(dataset);
datagridCustomers.DataSource = dataset;
}
catch (Exception EX)
{
HandleException(" 无法装入数据!", EX);
}
finally
{
if (adapter != null)
adapter.Dispose();
if (command != null)
command.Dispose();
} } private void menuDataLoad_Click(object sender, EventArgs e)
{
//装载数据
//LoadData();
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;namespace CustomerEditor
{
public partial class Form1 : Form
{
private OleDbConnection _connection; //添加连接属性
public OleDbConnection Connection
{
get
{
return _connection;
}
set
{
Disconnection();
_connection = value;
} }
//断开数据库连接
public void Disconnection()
{
if (_connection != null)
{
if (_connection.State != ConnectionState.Closed)
_connection.Close();
_connection = null;
} } //意外处理
public void HandleException(string message, Exception EX)
{
MessageBox.Show(this, string.Format("{0}\n{1}:{2}", message, EX.GetType().ToString(), EX.Message)); }
//连接数据库
public void Connect()
{
if (dialogOpenFile.ShowDialog(this) == DialogResult.OK)
{
try
{
string connectionString = string.Format(
"Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};User ID=;Password=;",dialogOpenFile.FileName);
//Create a connection
OleDbConnection newConnection = new OleDbConnection(connectionString);
newConnection.Open();
Connection = newConnection;
}
catch(Exception EX)
{
//
HandleException("无法创建数据连接", EX);
}
} } public Form1()
{
InitializeComponent();
} private void menuDataConnect_Click(object sender, EventArgs e)
{
Connect();
} //装载数据
public void LoadData()
{
//
if (_connection == null)
{
MessageBox.Show(this, "你必须连接一个数据库!");
return;
}
OleDbCommand command = null;
OleDbDataAdapter adapter = null;
try
{
command = _connection.CreateCommand();
command.CommandText = "person";
command.CommandType = CommandType.TableDirect;
adapter = new OleDbDataAdapter(command);
DataSet dataset = new DataSet();
adapter.Fill(dataset);
datagridCustomers.DataSource = dataset;
}
catch (Exception EX)
{
HandleException(" 无法装入数据!", EX);
}
finally
{
if (adapter != null)
adapter.Dispose();
if (command != null)
command.Dispose();
} } private void menuDataLoad_Click(object sender, EventArgs e)
{
//装载数据
//LoadData();
}
}
}
解决方案 »
- 基于TCP的P2PSocket通信
- 继承Component做控件,用线程触发控件事件,怎么实现啊
- C# 怎么调用 Shell_NotifyIcon隐藏任务栏中应用程序的图标?
- 构造方法重载
- 如何在非windows程序中,监控键盘,鼠标等设备?
- 求一段自动生成数据库的代码!!
- winform如何和Flash通信
- [讨论]SqlDataAdapter的四个命令对象与SqlCommand对象,大家用哪个比较多?
- 请问ref 和out参数的作用??
- 多线程让线程等待的问题。
- 在c#后台代码中生成了一个可编辑的datagrid 怎么获取不到其中的输入值呢?
- C#如何使用Raw Socket发送TCP报文呢?
{
//装载数据
LoadData();
}这个别用说。
如果person是连接字符串,就不要打引号啊
效果应该是在datagrid上显示一个table的,然后点击看到表的内容。。
datagridCustomers.DataMember = dataset.Table[0].TableName;
datagridCustomers.databind();
没有这个属性的。
编译通不过。
错误 1 “System.Windows.Forms.DataGridView”并不包含“DataBind”的定义 F:\我的文档\Visual Studio 2005\Projects\CustomerEditor\CustomerEditor\Form1.cs 104 35 CustomerEditor
datagridCustomers.DataMember = "person";
datagridCustomers.DataSource = dataset;
datagridCustomers.databind();
2.跟踪Select语句是否正确,可以把它到查询分析器里Run一下
3.把 datagridCustomers.DataSource = dataset;
改成: datagridCustomers.DataSource = dataset.Table[0];
command.CommandText = "select * from person";
adapter = new OleDbDataAdapter(command);...这样也不行。
如果不行,先看DataSet中是否有数据,如果没,则先用Execute()看下是否有返回结果!
public void Connect()
{
if (dialogOpenFile.ShowDialog(this) == DialogResult.OK)
{
try
{
string connectionString = string.Format(
"Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};User ID=;Password=;", dialogOpenFile.FileName);
//Create a connection
OleDbConnection newConnection = new OleDbConnection(connectionString);
newConnection.Open();
Connection = newConnection;
}
catch (Exception EX)
{
//
HandleException("无法创建数据连接", EX);
}
} }这里有没有错? 谢谢。
我的Access里有2个表的,应该来说会显示表person的啊 。
{
//
if (_connection == null)
{
MessageBox.Show(this, "你必须连接一个数据库!");
return;
}
OleDbCommand command = null;
OleDbDataAdapter adapter = null;
try
{
command = _connection.CreateCommand();
command.CommandText = "telbooks";//这里如果我表名改不存在的就提示表错,说明可以取得指定的表啊 。
command.CommandType = CommandType.TableDirect;
adapter = new OleDbDataAdapter(command);
DataSet dataset = new DataSet();
adapter.Fill(dataset);
datagridTelBook.DataSource = dataset;
}
catch (Exception EX)
{
HandleException(" 无法装入数据!", EX);
}
finally
{
if (adapter != null)
adapter.Dispose();
if (command != null)
command.Dispose();
} }奇怪了,Datagrid就是不提示表名。