PDA

View Full Version : سوال: ایجاد کلاسی برای کار با دیتا بیس



kh1387
دوشنبه 23 شهریور 1388, 16:32 عصر
با سلام
آیا میشه کلاسی ایجاد کرد که باز و بسته کردن اتصال و اجرا کردن کامنتهای اس کیو ال در آنجا انجام بپذیره؟
من این کار و رکردم ولی ارور میده:
روی cmd.execut.nonquery() میگه هنوز اتصال باز نشده.
در صورتیکه من در متدی این کار رو کردم و فراخوانی کردم.

keivan mousavi
سه شنبه 24 شهریور 1388, 00:33 صبح
دوست عزیز Profromance اد کن و خاصیتشو صدا کن مشکلت حل میشه

kh1387
سه شنبه 24 شهریور 1388, 22:17 عصر
دوست عزیز Profromance اد کن و خاصیتشو صدا کن مشکلت حل میشه
میشه در این زمینه بیشتر توضیح بدهید؟
متوجه منظورتون نمیشم:متفکر:
متشکر

RED-C0DE
چهارشنبه 25 شهریور 1388, 12:08 عصر
اصلا Connection رو به دیتابیس باز کردی قبل اجرا؟؟!

اگه مشکل حل نشد کد کلاست رو بذار اینجا...

Vahid_moghaddam
چهارشنبه 25 شهریور 1388, 12:42 عصر
یه Property توی کلاس برای کانکشن ایجاد کنید و دقت کنید اگر جایی بازش می کنید، بسته نشه. هیچ مشکلی نباید وجود داشته باشه. اگه با sql کار می کنید، از معماری سه لایه و strong typed dataset ها استفاده کنید

mtaboy
چهارشنبه 25 شهریور 1388, 13:52 عصر
این یک نمونه ...

kh1387
چهارشنبه 25 شهریور 1388, 15:41 عصر
یه Property توی کلاس برای کانکشن ایجاد کنید و دقت کنید اگر جایی بازش می کنید، بسته نشه. هیچ مشکلی نباید وجود داشته باشه. اگه با sql کار می کنید، از معماری سه لایه و strong typed dataset ها استفاده کنید
متشکرم از شما
من مشکلم همون معماری سه لایه هستش
اگه میشه مبتدی اینجا یه توضیح بدید یا یه مثال نمونه برام ارسال کنید ممنون می شم.
در ضمن مقاله و جزوه هم در موردش خوندم ولی درست یاد نگرفتم.
اگه پروژه عملی دارید ممنون می شم.:لبخندساده:

kh1387
چهارشنبه 25 شهریور 1388, 15:44 عصر
این یک نمونه ...
متشکرم
ولی این خیلی مبتدیه و ساده
حرفه ای تر میخوام
باز هم از زحماتتون متشکرم

ali-kh
سه شنبه 21 مهر 1388, 21:16 عصر
اگه میشه کدی که نوشتید رو بزارید تا ببینیم
این یه نمونه ساده که من چند سال پیش نوشته بودم.
اگه مایل باشید با کمک هم دیگه یک کد ترو تمیز بسته به نیاز های خودمون بنویسیم
چون واسه هر پروژه دیتا بیسی لازمه

using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

/// <summary>
/// Class contains generic data access functionality to be accessed from
/// the business tier
/// </summary>
public static class GenericDataAccess
{


public static string username = "sa", password = "admin", database = "tx";
// static constructor
static GenericDataAccess()
{
//
// TODO: Add constructor logic here
//
}

// execute a command and return the results as a DataTable object
public static DataTable ExecuteSelectCommand(SqlCommand command)
{
// The DataTable to be returned
DataTable table;
// Execute the command making sure the connection gets closed in the end
try
{
// Open the data connection
command.Connection.Open();
// Execute the command and save the results in a DataTable
SqlDataReader reader = command.ExecuteReader();
table = new DataTable();
table.Load(reader);
// Close the reader
reader.Close();
}
catch (Exception ex)
{

throw ex;
}
finally
{
// Close the connection
command.Connection.Close();
}
return table;
}

// execute an update, delete, or insert command
// and return the number of affected rows
public static int ExecuteNonQuery(SqlCommand command)
{
// The number of affected rows
int affectedRows = -1;
// Execute the command making sure the connection gets closed in the end
try
{
// Open the connection of the command
command.Connection.Open();
// Execute the command and get the number of affected rows
affectedRows = command.ExecuteNonQuery();
}
catch (Exception ex)
{
// Log eventual errors and rethrow them

throw ex;
}
finally
{
// Close the connection
command.Connection.Close();
}
// return the number of affected rows
return affectedRows;
}

// execute a select command and return a single result as a string
public static string ExecuteScalar(SqlCommand command)
{
// The value to be returned
string value = "";
// Execute the command making sure the connection gets closed in the end
try
{
// Open the connection of the command
command.Connection.Open();
// Execute the command and get the number of affected rows
value = command.ExecuteScalar().ToString();
}
catch (Exception ex)
{
// Log eventual errors and rethrow them

throw ex;
}
finally
{
// Close the connection
command.Connection.Close();
}
// return the result
return value;
}

// creates and prepares a new SqlCommand object on a new connection
public static SqlCommand CreateCommand()
{

try
{
// Obtain the database provider name
//string dataProviderName = "System.Data.SqlClient";
// Obtain the database connection string

string connectionString = string.Format("Server=server;Database={0};password={1};user={2}", database, password, username);
// Create a new data provider factory
//DbProviderFactory factory = DbProviderFactories.GetFactory(dataProviderName);
// Obtain a database specific connection object
SqlConnection conn = new SqlConnection(connectionString);//= factory.CreateConnection();
// Set the connection string
//conn.ConnectionString = connectionString;
// Create a database specific command object
SqlCommand comm = conn.CreateCommand();
// Set the command type to stored procedure
comm.CommandType = CommandType.StoredProcedure;
// Return the initialized command object
return comm;
}
catch (Exception ex)
{
return null;
}
}
}

kh1387
چهارشنبه 22 مهر 1388, 14:20 عصر
بالاخره دیشب به هر بدبختی بود اینقده کار کردم تا روش کار رو یاد گرفتم:تشویق::تشویق::تشویق::ق ب::بوس: