2013-10-10 2 views
0

문제가 있습니다. 문제는 프로젝트 전반에서 동일한 db 연결을 사용하고 싶습니다. 별도의 클래스를 만들어야한다고 생각합니다. 내가 원하는 것 : Sqliteconnection 구성 요소와 같은 것을 원하지만 MYSQL의 경우. mysql 데이터베이스와의 연결을 처리하고 코드의 모든 곳에서 액세스 할 수있는 클래스. 이런 종류의 검색 엔진을 사용하여 운이 좋았습니다. 저는 C#에 대해 꽤 새롭기 때문에 당신의 도움에 감사드립니다!별도의 클래스에있는 # mysqlDb 연결

+0

http://dev.mysql.com/downloads/connector/net/ –

+0

이 커넥터는 이미 가지고 있지만이 커넥터는 devart sqlite와 같은 VS 구성 요소를 제공하지 않습니다. 또는 뭔가를 놓치고있어, 나는 VS (C#)에서 구성 요소를 찾을 수 없습니다. 그게 내가 여기서 도움을 청하는 이유입니다. – Rikcon

+0

구성 요소는 무엇을 의미합니까? 당신은 일반적인 클래스를 찾고 있었다 - 그것은 MySqlConnection, MySqlCommand, MySqlDataReader 등을 가지고있다. DB 접근을위한 모든 공통 .NET 클래스 –

답변

1

다음은 SQL 서버용 db 연결을 생성하는 클래스입니다. MySQL의 경우 적절한 변경을해야합니다.

public class SQLServer 
{ 
    SqlConnection myConnection = null; 
    SqlCommand myCommand = null; 
    /// <summary> 
    /// Connection information should be stored in the appp.conf 
    /// Example: 
    /// <add name="SQLConnection" connectionString="Data Source=server;Initial Catalog=YourDatabase; 
    /// Integrated Security="True" providerName="System.Data.SqlClient"/> 
    /// </summary> 
    public SQLServer() 
    { 
     string myConfig = ConfigurationManager.ConnectionStrings["SQLConnection"].ConnectionString; 
     myConnection = new SqlConnection(myConfig); 
     myConnection.Open(); 
    } 

    //Executes sql query and returns datareader  
    public SqlDataReader NewReader(string sSQL) 
    { 
     myCommand = new SqlCommand(sSQL, myConnection); 
     SqlDataReader myReader = myCommand.ExecuteReader(); 
     return myReader; 
    } 

    //Execute sql statement and returns nothing usefull for inserts and updates 
    public void Execute(string SQL) 
    { 
     Execute(SQL, false); 
    } 
} 

클래스를 사용하려면;

SQLServer myClass = new SQLServer(); 
SqlDataReader myReader = myClass.NewReader("SQL Syntax"); 
while (myReader.Read()) 
{ 
    MessageBox.Show(myReader["some_field"].ToString()) 
} 

나는 오류 처리를 생략하고 연결을 닫으므로 문제가 발생하지 않도록하십시오.

+0

대단히 감사합니다. – Rikcon

관련 문제