**
以下是获取数据库数据的两种写法:
**
string dataSource = this.tbx_source.Text.Trim(); //输入连接的数据库
string userId = this.tbx_ID.Text.Trim(); //数据库用户
string password = this.tbx_password.Text.Trim(); //密码
string connectString = "Data Source=" + dataSource + ";user id=" + userId + ";password=" + password + ";Unicode=True"; //连接字符串
OracleConnection conn = new OracleConnection(connectString);//构建连接类
string selectCommand = "select * from infos"; //sql语句
OracleDataReader reader = null;
try
{
conn.Open(); //打开orcle的连接
OracleCommand command = new OracleCommand(selectCommand, conn);
// 捆绑方法之一,利用BindingSource
reader = command.ExecuteReader(); //执行sql语句
//BindingSource bs = new BindingSource(); //捆绑源
//bs.DataSource = reader; //捆绑执行结果
//this.dataGridView1.DataSource = bs; //将datagridview与dataReader捆绑
//捆绑方法之二,利用datatable
DataTable table = new DataTable();
table.Load(reader); //将执行结果导入表中
this.dataGridView1.DataSource = table; //捆绑数据
}
catch (Exception ex1)
{
MessageBox.Show("获取数据失败");
}
finally
{
reader.Close();
conn.Close();
}
---------------------
作者:lzmings
来源:CSDN
原文:https://blog.csdn.net/lzmings/article/details/44115957
版权声明:本文为博主原创文章,转载请附上博文链接!