关于数据库连接(datagrid,listview)

数据库连接主要就是oracle,sql,access数据库的连接,

最近oracle数据库的连接也成功了,但是在数据显示,主要想用datagrid,listview把数据显示出来,并做相应的修改方面还欠缺.不过也是一个学习的过程,一直在找这方面的资料和做测试

Access 连接:

using System.Data.OleDb ;

OleDbConnection dbconn=new OleDbConnection (@"Provider=Microsoft.Jet.OLEDB.4.0;Data source=e:\test.mdb");
dbconn.Open ();
OleDbDataAdapter myAdapter=new OleDbDataAdapter ("select student_id,name,sex,birthday from student",dbconn);
OleDbCommandBuilder myBuilder=new OleDbCommandBuilder (myAdapter);
DataSet myDataSet=new DataSet ();
myAdapter.Fill (myDataSet,"student");
dataGrid1.DataSource =myDataSet.Tables ["student"];
dbconn.Close ();

通过这段简单的代码就能所要查询的数据在dataGrid中显示出来了

oracle 连接:

using Oracle.DataAccess .Client ;

/ be sure to replace OTNSRVR with your host's name
string oradb = "Data Source=(DESCRIPTION=" // C#
+ "(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=zhulin1)(PORT=1521)))"
+ "(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=orazl)));"
+ "User Id=ems_user;Password=emssme;";

OracleConnection conn = new OracleConnection(oradb);
try
{
conn.Open();

OracleCommand cmd = new OracleCommand();
cmd.Connection = conn;
// Lab 1 select string
//cmd.CommandText = "select dname from dept where deptno = 10";
// Lab 2 select string
//cmd.CommandText = "select deptno, dname, loc from dept where deptno = " + textBox1.Text;
// Lab 3 select string
//add

//OracleCommandBuilder builder=new OracleCommandBuilder ();
cmd.CommandText = "select trade_mode,ems_no,du_mark from ems_dcr_head where rownum<10";


cmd.CommandType = CommandType.Text;

OracleDataReader dr = cmd.ExecuteReader();

// Labs 1 and 2 data retrieval
//dr.Read();
//
// Lab 1 assign data to label
//label1.Text = dr.GetString(0); // .NET native type
//label1.Text = dr.GetOracleString(0).ToString(); // Oracle native type
// Lab 2 assign data to label
//label1.Text = dr.GetString(1); // .NET native type
//label1.Text = dr.GetOracleString(1).ToString(); // Oracle native type

// Lab 3 data retrieval
while (dr.Read())
{

listBox1.Items.Add(dr.GetString (0)+" The " + dr.GetString(1) +
" department is in " + dr.GetString(2));
}
}
catch (OracleException ex) // catches only Oracle errors
{
switch (ex.Number)
{
case 1:
MessageBox.Show("Error attempting to insert duplicate data.");
break;
case 12545:
MessageBox.Show("The database is unavailable.");
break;
default:
MessageBox.Show("Database error: " + ex.Message.ToString());
break;
}
}
catch (Exception ex) // catches any error
{
MessageBox.Show(ex.Message.ToString());
}
finally
{
conn.Dispose(); // C#
}

这段代码是拷贝oracle网站帮助上的,在我的回复里也有相关网址.

通过这段代码可以让数据在listbox中显示出来,在datagrid中显示还有一定的问题.哪位高手在这方面知道多一点

的也不妨把东西写出来共享一下.

**************************************

查了一些资料,这一段代码也应该可以,可是老是出现问题

string oradb = "Data Source=(DESCRIPTION=" // C#
+ "(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=zhulin1)(PORT=1521)))"
+ "(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=orazl)));"
+ "User Id=ems_user;Password=emssme;";
OracleConnection conn = new OracleConnection(oradb);
conn.Open ();

OracleDataAdapter thisAdapter=new OracleDataAdapter("select trade_mode,ems_no,du_mark from ems_dcr_head where rownum<10",conn);
OracleCommandBuilder thisBuilder=new OracleCommandBuilder(thisAdapter);
DataSet thisDataSet=new DataSet ();

thisAdapter.Fill (thisDataSet,"ems_dcr_head");
//Console.WriteLine ("you{0}",thisDataSet.Tables["ems_dcr_head"].Rows[2]["ems_no"]);
conn.Close ();

这些问题只能以后慢慢解决了.sql数据库和这个代码查不多,连接字符串不一样而已

祝大家:周末愉快!明天可以睡个懒觉了

你可能感兴趣的:(oracle,sql,c,SQL Server,Access)