MSAccess连接类 [英] MSAccess Connectivity Class

查看:79
本文介绍了MSAccess连接类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的小型C#项目中,我正在编写用于执行查询和连接到单个类中的数据库的代码,但我想保持连接类独立并调用connect方法,无论我需要怎样才能帮助我。 。

解决方案

你好Raghavendra,

您应该尝试使用OleDBConnection或ODBCConnection类 - 它已经由MS编写,因此您不需要尝试发明已经发明的东西。



如果您决定使用ODBC - 请检查以下内容:

http://msdn.microsoft.com/en-us/library/system.data.odbc.odbcconnection.aspx [ ^ ]

http:// msdn。 microsoft.com/en-us/library/system.data.odbc.odbcdatareader.aspx [ ^ ]



如果您决定使用OleDB - 请查看以下内容:

http:// msdn.microsoft.com/en-us/library/system.data.oledb.oledbconnection.aspx [ ^ ]

http://msdn.microsoft.com/en-us/library/system.data.oledb.oledbdatareader.aspx [ ^ ]



还有另一种方法 - 使用Microsoft.Office.Interop.Access,我不建议这样做,因为这种方法创建了一个MS Access对象的实例。但是你可以谷歌吧。



干杯!



Modestas


< blockquote>请尝试这样:

a db class和另一个带有Inser函数的类调用它:

  public   class  DBLayer 
{
private SqlConnection康涅狄格州;
public SqlConnection OpenConnection()
{
SqlConnection connection = null ;
string strConnection = ConfigurationSettings.AppSettings [ 的ConnectionString];
try
{
connection = new SqlConnection(strConnection);
connection.Open();
}
catch (例外情况)
{
throw ;
}
返回连接;
}
}

public class MyForm
{
private bool Insert()
{
try
{
DBLayer objDBLayer = new DBLayer();

SqlCommand command = new SqlCommand( insert query goes here,objDBLayer.OpenConnection());
command.ExecuteNonQuery();
return true ;
}
catch
{
return ;
}
}
}


In my small C# project i am writing both code for executing queries and connection to a databse in a single class but i want to maintain connection class separate and call connect method where ever i required how can i do it please help me . .

解决方案

Hello Raghavendra,
You should try using OleDBConnection or ODBCConnection classes - it's already written by MS so you should not need to try inventing something which already invented.

If you decide to go with ODBC - please check the bellow:
http://msdn.microsoft.com/en-us/library/system.data.odbc.odbcconnection.aspx[^]
http://msdn.microsoft.com/en-us/library/system.data.odbc.odbcdatareader.aspx[^]

if you decide to go with OleDB - please check the bellow:
http://msdn.microsoft.com/en-us/library/system.data.oledb.oledbconnection.aspx[^]
http://msdn.microsoft.com/en-us/library/system.data.oledb.oledbdatareader.aspx[^]

Also there is another way to go - using Microsoft.Office.Interop.Access, which I would not recommend as this approach creates an instance of MS Access object. But you can google it.

Cheers!

Modestas


hi, try like this:
a db class and another class with a Inser function calling it:

public class DBLayer
   {
       private SqlConnection conn;
       public SqlConnection OpenConnection()
       {
           SqlConnection connection = null;
           string strConnection = ConfigurationSettings.AppSettings["ConnectionString"];
           try
           {
               connection = new SqlConnection(strConnection);
               connection.Open();
           }
           catch (Exception ex)
           {
               throw;
           }
           return connection;
       }
   }

   public class MyForm
   {
       private bool Insert()
       {
           try
           {
               DBLayer objDBLayer = new DBLayer();

               SqlCommand command = new SqlCommand("insert query goes here", objDBLayer.OpenConnection());
               command.ExecuteNonQuery();
               return true;
           }
           catch
           {
               return false;
           }
       }
   }


这篇关于MSAccess连接类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆