Hej Udviklere,
jeg er i gang med at udvikle min egenklasse, som skal varetage kommunikation med SQL Server 2005 i mine projekter (skole projekter). Klassen ser ud som følgende:
- using System;
- using System.Collections.Generic;
- using System.Data;
- using System.Data.Sql;
- using System.Data.SqlClient;
-
- namespace Mssql
- {
- public class SqlClass
- {
- static SqlClass instance = null;
-
- DataTable dTable = null;
- SqlDataAdapter sqlAdapter = null;
- SqlConnection connection = null;
-
- private SqlClass()
- {
- connection = new SqlConnection();
- }
-
- public int NonQuery(string query)
- {
- SqlCommand sqlCommand = this.connection.CreateCommand();
- sqlCommand.CommandType = CommandType.Text;
- sqlCommand.CommandText = query;
-
- return sqlCommand.ExecuteNonQuery();
- }
-
- public string Close()
- {
- this.connection.Close();
-
- return connection.State.ToString();
- }
-
- public string Open()
- {
- this.connection.Open();
-
- return connection.State.ToString();
- }
-
- public string[,] ToArray()
- {
- string[,] data = new string[this.Columns, this.Rows];
-
- for(int i = 0; i < this.Columns; i++)
- {
- for(int j = 0; j < this.Rows; j++)
- {
- data[i, j] = this.dTable.Rows[i][j].ToString();
- }
- }
-
- return data;
- }
-
- public void Query(string query)
- {
- SqlCommand sqlCommand = this.connection.CreateCommand();
- sqlCommand.CommandType = CommandType.Text;
- sqlCommand.CommandText = query;
-
- this.dTable = new DataTable();
- this.sqlAdapter = new SqlDataAdapter(sqlCommand);
- this.sqlAdapter.Fill(this.dTable);
- }
-
- public void SetInfo(string dataSource, string catalog)
- {
- string connString = "Data Source=" + dataSource + "; Initial Catalog=" + catalog + "; Integrated Security=SSPI;";
-
- this.connection.ConnectionString = connString;
- }
-
- public void SetInfo(string dataSource, string catalog, string userId, string password)
- {
- string connString = "Data Source=" + dataSource + "; Initial Catalog=" + catalog + "; User ID=" + userId + "; Password=" + password + ";";
-
- this.connection.ConnectionString = connString;
- }
-
- public void StoredProcedure(string procedure, string[,] parameters)
- {
- SqlCommand sqlCommand = this.connection.CreateCommand();
- sqlCommand.CommandType = CommandType.StoredProcedure;
- sqlCommand.CommandText = procedure;
-
- for (int i = 0; i < parameters.GetLength(0); i++)
- {
- SqlParameter sqlParam = new SqlParameter("@" + parameters[i, 0], parameters[i, 1]);
- sqlCommand.Parameters.Add(sqlParam);
- }
-
- this.dTable = new DataTable();
- this.sqlAdapter = new SqlDataAdapter(sqlCommand);
- this.sqlAdapter.Fill(this.dTable);
- }
-
- public DataTable Fetch()
- {
- return this.dTable;
- }
-
- public static SqlClass getInstance()
- {
- if (instance == null)
- instance = new SqlClass();
- return instance;
- }
-
- public int Columns
- {
- get
- {
- return this.dTable.Columns.Count;
- }
- }
-
- public int Rows
- {
- get
- {
- return this.dTable.Rows.Count;
- }
- }
- }
- }
Jeg har ikke lavet specifikke funktioer, som laver en "select" eller "update" som man kender i nogle frameworks (codeigniter til php).
Spørgsmålet går nu på, er der noget som I mener jeg mangler i klassen for at gøre den mere "komplet"? Desuden så er jeg ikke helt sikker på hvordan jeg skal fange OUT parametere i Stored Procedures, men vil min nuværende metode kunne dette?
Indlæg senest redigeret d. 03.02.2010 10:06 af Bruger #6559