请问大家一个问题:在C#中如何使用ADO.net?

xmzhy 2004-04-02 09:14:00
本人问过许多人,发现大家在C#中对数据库编程很少用ADO.NET编写,请问如何用ADO.NET实现数据库的基本操作?
...全文
63 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
很驚訝樓主問的那些人,不知道他們使用什麼來連接並操作數據庫...可否詳細說明一下.
bearbaba 2004-04-02
  • 打赏
  • 举报
回复
最简单的ado.net操作
public int iDBConnect( )
{
string sConn = PMCon.sGetPMConn(); // 数据连接
int _CheckState = 0; // 数据库状态检查,正确为0,不正确为1;
SqlConnection PMConnection = new SqlConnection(sConn);
try
{
PMConnection.Open();
}
catch(Exception E_SQLERROR)
{
_CheckState = -1;
er.vWrLog(E_SQLERROR,er.ErrorMsg,1); // 日志,并改写状态
}
finally
{
PMConnection.Close();
}
return _CheckState;
}
brargil 2004-04-02
  • 打赏
  • 举报
回复
在MSDN中,.net的数据库连接字符串都有详细的说明,我这里以代码范例的方式罗列一些,具体的每一项代表的意义可以参看MSDN.
ADO.net 中数据库连接方式(微软提供)
微软提供了以下四种数据库连接方式:
System.Data.OleDb.OleDbConnection
System.Data.SqlClient.SqlConnection
System.Data.Odbc.OdbcConnection
System.Data.OracleClient.OracleConnection
下面我们以范例的方式,来依次说明:

System.Data.SqlClient.SqlConnection
常用的一些连接字符串(C#代码):
程序代码:

SqlConnection conn = new SqlConnection( "Server=(local);Integrated Security=SSPI;database=Pubs");

SqlConnection conn = new SqlConnection("server=(local)\\NetSDK;database=pubs;Integrated Security=SSPI");

SqlConnection conn = new SqlConnection("Data Source=localhost;Integrated Security=SSPI;Initial Catalog=Northwind;");

SqlConnection conn = new SqlConnection(" data source=(local);initial catalog=xr;integrated security=SSPI;
persist security info=False;workstation id=XURUI;packet size=4096; ");

SqlConnection conn = new System.Data.SqlClient.SqlConnection("Persist Security Info=False;Integrated
Security=SSPI;database=northwind;server=mySQLServer");

SqlConnection conn = new SqlConnection( " uid=sa;pwd=passwords;initial catalog=pubs;data source=127.0.0.1;Connect Timeout=900");


更多字符串连接说明请看MSDN:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemDataSqlClientSqlConnectionClassConnectionStringTopic.asp

System.Data.OleDb.OleDbConnection
常用的一些连接字符串(C#代码):
程序代码:

OleDbConnection conn = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\MyWeb\81\05\GrocerToGo.mdb");

OleDbConnection conn = new OleDbConnection(
@"Provider=Microsoft.Jet.OLEDB.4.0;Password=;
User ID=Admin;Data Source=grocertogo.mdb;");

OleDbConnection conn = new OleDbConnection(
"Provider=MSDAORA; Data Source=ORACLE8i7;Persist Security Info=False;Integrated Security=yes");

OleDbConnection conn = new OleDbConnection(
"Provider=Microsoft.Jet.OLEDB.4.0; Data Source=c:\bin\LocalAccess40.mdb");

OleDbConnection conn = new OleDbConnection(
"Provider=SQLOLEDB;Data Source=MySQLServer;Integrated Security=SSPI");

更多字符串连接说明请看MSDN:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemDataOleDbOleDbConnectionClassConnectionStringTopic.asp?frame=true

System.Data.OracleClient.OracleConnection
常用的一些连接字符串(C#代码):
程序代码:

OracleConnection myConn = new System.Data.OracleClient.OracleConnection(
"Data Source=Oracle8i;Integrated Security=yes");

更多字符串连接说明请看MSDN:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemDataOracleClientOracleConnectionClassConnectionStringTopic.asp?frame=true

System.Data.Odbc.OdbcConnection
常用的一些连接字符串(C#代码):
程序代码:

OdbcConnection conn = new OdbcConnection(
"Driver={SQL Server};Server=MyServer;Trusted_Connection=yes;Database=Northwind;");

OdbcConnection conn = new OdbcConnection(
"Driver={Microsoft ODBC for Oracle};Server=ORACLE8i7;
Persist Security Info=False;Trusted_Connection=yes");

OdbcConnection conn = new OdbcConnection(
"Driver={Microsoft Access Driver (*.mdb)};DBQ=c:\bin\nwind.mdb");

OdbcConnection conn = new OdbcConnection(
"Driver={Microsoft Excel Driver (*.xls)};DBQ=c:\bin\book1.xls");

OdbcConnection conn = new OdbcConnection(
"Driver={Microsoft Text Driver (*.txt; *.csv)};DBQ=c:\bin");

OdbcConnection conn = new OdbcConnection("DSN=dsnname");

更多字符串连接说明请看MSDN:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemDataOdbcOdbcConnectionClassConnectionStringTopic.asp?frame=true

其他厂商提供的数据库连接:
程序代码:

DB2Connection myConn = new IBM.Data.DB2.DB2Connection("DATABASE = SAMPLE;UID=<username>; PWD=<password>;");

DB2Connection myConn = new IBM.Data.DB2.DB2Connection("DATABASE = SAMPLE");

BdpConnection myConn = new Borland.Data.Provider.BdpConnection("assembly=Borl
and.Data.Mssql,Version=1.1.0.0,Culture=neutral,PublicKeyToken=91d62ebb5b0d1b1b;ve
ndorclient=sqloledb.dll;osauthentication=False;database=<database>;usernam
e=<user>;hostname=<host>;password=<password>;provider=MSSQL");

BdpConnection myConn = new Borland.Data.Provider.BdpConnection("assembly=Borl
and.Data.Db2,Version=1.1.0.0,Culture=neutral,PublicKeyToken=91d62ebb5b0d1b1b;ve
ndorclient=db2cli.dll;database=<database>;username=<user>;
password=<password>;provider=DB2");

Connection Pooling

在SQL Server、OLE DB和.NET框架结构中的Data Provider中,都提供了隐式的连接池连接支持。你可以在ConnectionString中指定不同的参数值控制连接池的行为。比如下面的例子使OLE DB的连接池无效并自动地进行事务处理:
Provider=SQLOLEDB;OLE DB Services=-4;Data Source=localhost;Integrated Security=SSPI;
在SQL Server.NET Data Provider中提供了以下参数设置控制连接池的行为:Connection Lifttime、Connection Reset、Enlist、Max Pool Size、Min Pool Size和Pooling。

更多数据库连接信息,以及非ADO.net的连接字符串可以参看:
http://www.connectionstrings.com/
Paperback: 1625 pages Publisher: Apress; 7th ed. 2015 edition (January 1, 2016) Language: English ISBN-10: 1484213335 ISBN-13: 978-1484213339 This new 7th edition of Pro C# 6.0 and the .NET 4.6 Platform has been completely revised and rewritten to reflect the latest changes to the C# language specification and new advances in the .NET Framework. You'll find new chapters covering all the important new features that make .NET 4.6 the most comprehensive release yet, including: A Refined ADO.NET Entity Framework Programming Model Numerous IDE and MVVM Enhancements for WPF Desktop Development Numerous updates to the ASP.NET Web APIs This comes on top of award winning coverage of core C# features, both old and new, that have made the previous editions of this book so popular. Readers will gain a solid foundation of object-oriented development techniques, attributes and reflection, generics and collections as well as numerous advanced topics not found in other texts (such as CIL opcodes and emitting dynamic assemblies). The mission of this book is to provide you with a comprehensive foundation in the C# programming language and the core aspects of the .NET platform plus overviews of technologies built on top of C# and .NET (ADO.NET and Entity Framework, Windows Communication Foundation (WCF), Windows Presentation Foundation (WPF), ASP.NET (WebForms, MVC, WebAPI).). Once you digest the information presented in these chapters, you’ll be in a perfect position to apply this knowledge to your specific programming assignments, and you’ll be well equipped to explore the .NET universe on your own terms. What you’ll learn Be the first to understand the .NET 4.6 platform and C# 6. Discover the ins and outs of the leading .NET technology. Learn from an award-winning author who has been teaching the .NET world since version 1.0. Find complete coverage of XAML, .NET 4.6 and Visual Studio 2015 together with discussion of the new Windows Runtime. Who this book is for This book is perfect for anyone who is interested in the new .NET Framework 4.6 and the C# language. Whether you are moving to .NET for the first time or are already writing applications using previous .NET versions, this book will provide you with a comprehensive grounding in the new technology and serve as a complete reference throughout your coding career.

110,533

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧