`

关于数据库连接(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数据库和这个代码查不多,连接字符串不一样而已

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

分享到:
评论

相关推荐

    c#sql2000列车信息查询

    --带数据库 可以运行 VS...--XML读取数据库连接串 --存储过程 --SQL函数 --视图 --ListView --DataGrid ----打印 --DataSet填充打印数据 --用到视图 作为存储过程在水晶报表里的 数据机构映射 ---邮箱flyinget@126.com

    asp.net知识库

    关于能自定义格式的、支持多语言的、支持多数据库的代码生成器的想法 发布Oracle存储过程包c#代码生成工具(CodeRobot) New Folder XCodeFactory3.0完全攻略--序 XCodeFactory3.0完全攻略--基本思想 XCodeFactory...

    Visual Basic 6编程技术大全 中译本扫描版带书签 2/2

    第2章关于窗体20 2.1 通用属性20 2.1.1 Left、Top、Width和Height属性21 2.1.2 ForeColor和BackColor属性21 2.1.3 Font属性23 2.1.4 Caption和Text属性24 2.1.5 Parent和Container属性25 2.1.6 Enabled和Visible属性...

    Visual Basic 6编程技术大全 中译本扫描版带书签 1/2

    第2章关于窗体20 2.1 通用属性20 2.1.1 Left、Top、Width和Height属性21 2.1.2 ForeColor和BackColor属性21 2.1.3 Font属性23 2.1.4 Caption和Text属性24 2.1.5 Parent和Container属性25 2.1.6 Enabled和Visible属性...

    net学习笔记及其他代码应用

    31.公司要求开发一个继承System.Windows.Forms.ListView类的组件,要求达到以下的特殊功能:点击ListView各列列头时,能按照点击列的每行值进行重排视图中的所有行 (排序的方式如DataGrid相似)。根据您的知识,请...

Global site tag (gtag.js) - Google Analytics