PDA

View Full Version : مشکل در اتصال به بانک



mortezawolf
یک شنبه 24 خرداد 1388, 12:27 عصر
سلام دوستان
من به یه مشکل تو اتصال به بانک و insert کردن برخوردم
من یه صفحه بنام dataaccess.cs دارم به شرح زیر:

using System;
using System.Data;
using System.Data.OleDb;
using System.Configuration;


/// <summary>
/// Summary description for dataaccess
/// </summary>
public class dataaccess
{
protected static string connectionString;
public string defaultDBName = "Db_Chamran";
public string DBName;
public string server = "localhost";
public string DBMS = "SQLSERVER";
public string username = "";
public string password = "";
public OleDbConnection connection;
protected static OleDbCommand insertCommand;
protected static OleDbCommand deleteCommand;
protected static OleDbCommand selectCommand;
protected static OleDbCommand updateCommand;
protected static OleDbDataAdapter adapter;
protected static DataSet dataSet = new DataSet();
protected static DataTable dt;
public string error = "";

public dataaccess(string database)
{
connection = new OleDbConnection();
insertCommand = new OleDbCommand("", connection);
deleteCommand = new OleDbCommand("", connection);
selectCommand = new OleDbCommand("", connection);
updateCommand = new OleDbCommand("", connection);
adapter = new OleDbDataAdapter(selectCommand);
DBName = database;
openConnection();
}

public dataaccess()
{
connection = new OleDbConnection();
insertCommand = new OleDbCommand("", connection);
deleteCommand = new OleDbCommand("", connection);
selectCommand = new OleDbCommand("", connection);
updateCommand = new OleDbCommand("", connection);
adapter = new OleDbDataAdapter(selectCommand);
DBName = defaultDBName;
openConnection();
}

public void openConnection()
{
try
{
createConnectionString();
if (connection.State != ConnectionState.Closed)
connection.Close();
connection.ConnectionString = connectionString;
connection.Open();
}
catch (Exception exp)
{
}
}

public void resetConnection(string database)
{
try
{
if (connection.State == ConnectionState.Open)
connection.Close();
DBName = database;
openConnection();
}
catch (Exception exp)
{
}
}

public void createConnectionString()
{
DBMS = DBMS.ToLower();
switch (DBMS)
{
case "access": connectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + DBName; break;
// case "sqlserver": connectionString = @"Provider=SQLOLEDB;Data Source=" + server + ";Integrated Security=SSPI;Initial Catalog=" + DBName; break;
case "sqlserver": connectionString = @"Provider = sqloledb;Server=.; Data Source =" + server + "; Initial Catalog = " + DBName + ";Integrated Security=true; User Id = " + username + "; Password = " + password + ";"; break;
case "dsn": connectionString = "DSN=" + DBName; break;
}
}

private int getNewID(string tableName)
{
try
{
selectCommand.CommandText = "SELECT MAX(fldid) FROM tbl" + tableName + " ;";
if (dataSet.Tables["newid"] != null)
dataSet.Tables["newid"].Clear();

adapter.Fill(dataSet, "newid");

int id = (dataSet.Tables["newid"].Rows[0][0] != DBNull.Value) ? Convert.ToInt32(dataSet.Tables["newid"].Rows[0][0]) : 0;
return ++id;
}
catch (Exception exp)
{
return 0;
}
}
/*
public int addEntity(Entity m)
{
try
{

int id = getNewID(m.entityName);
m.ID = id;

string command = "INSERT INTO tbl" + m.entityName + "(";
for (int i = 0; i < m.fieldNames.Length; i++)
{
command += "fld" + m.fieldNames[i];
if (i < m.fieldNames.Length - 1)
command += ", ";
else
command += ") VALUES(";
}

for (int i = 0; i < m.fieldNames.Length; i++)
{
command += m.fieldValues[i];
if (i < m.fieldNames.Length - 1)
command += ", ";
else
command += ");";
}

insertCommand.CommandText = command;
insertCommand.ExecuteNonQuery();
return id;
}
catch (Exception exp)
{
return -1;
}
}

public bool updateEntity(Entity m)
{
try
{
string command = "UPDATE tbl" + m.entityName + " SET ";
for (int i = 1; i < m.fieldNames.Length; i++)
{
command += "fld" + m.fieldNames[i];
command += " = ";
command += m.fieldValues[i];
if (i < m.fieldNames.Length - 1)
command += ", ";
else
command += "WHERE fld" + m.fieldNames[0] + " = " + m.fieldValues[0];
}
updateCommand.CommandText = command;
updateCommand.ExecuteNonQuery();
return true;
}
catch (Exception exp)
{
return false;
}
}

public bool getEntity(int id, Entity m)
{
try
{
selectCommand.CommandText = "SELECT * FROM tbl" + m.entityName + " WHERE fldid = " + id + " ;";

if (dataSet.Tables["entity"] != null)
dataSet.Tables["entity"].Clear();

adapter.Fill(dataSet, "entity");

DataTable dt = dataSet.Tables["entity"];

if (dt.Rows.Count == 0)
return false;

string value = "";

for (int i = 0; i < m.fieldNames.Length; i++)
{
value = (dt.Rows[0]["fld" + m.fieldNames[i]] != DBNull.Value) ? Convert.ToString(dt.Rows[0]["fld" + m.fieldNames[i]]) : "";
m.setPeroperyValue(m.fieldNames[i], value);
}

return true;
}
catch (Exception exp)
{
return false;
}
}
*/
public DataSet getList(string tableName, string[] fields, string condition, string group, string order, bool rowNumber)
{
try
{
string command = "";
command = "SELECT ";

if (rowNumber)
command += "ROW_NUMBER() OVER (ORDER BY " + order + ") AS [fldrow],";

for (int i = 0; i < fields.Length; i++)
{
command += (fields[i].IndexOf("fld") < 0) ? "fld" : "";
command += fields[i];
if (i < fields.Length - 1)
command += ", ";
}
command += " FROM tbl" + tableName + " ";

if (condition != null)
command += "WHERE " + condition + " ";

if (group != null)
command += "GROUP BY " + group + " ";

if (order != null)
command += "ORDER BY " + order + " ";

command += ";";

selectCommand.CommandText = command;

DataSet ds = new DataSet();
adapter.Fill(ds, "list");
return ds;
}
catch (Exception exp)
{
return null;
}
}

public DataSet getList(string tableName, string[] fields, string condition, string order, bool rowNumber)
{
return getList(tableName, fields, condition, null, order, rowNumber);
}

/* public Pair getPairList(string tableName, string IDField, string nameField, string valueField, string condition, string group, string order)
{

DataSet ds;
if (valueField == null)
ds = getList(tableName, new string[] { IDField, nameField }, condition, group, order, false);
else
ds = getList(tableName, new string[] { IDField, nameField, valueField }, condition, group, order, false);

if (ds.Tables["list"] == null)
return null;

dt = ds.Tables["list"];
Pair p = new Pair(dt.Rows.Count);
for (int i = 0; i < dt.Rows.Count; i++)
{
p.ids[i] = Convert.ToInt32(dt.Rows[i][0]);
p.names[i] = (dt.Rows[i][1] != DBNull.Value) ? Convert.ToString(dt.Rows[i][1]) : "";
if (valueField != null)
p.values[i] = (dt.Rows[i][2] != DBNull.Value) ? Convert.ToInt32(dt.Rows[i][2]) : 0;
}

return p;

}

public Pair getPairList(string tableName, string nameField, string valueField, string condition, string group, string order)
{
return getPairList(tableName, "id", nameField, valueField, condition, group, order);
}
*/
public DataSet getResult(string query)
{
try
{
selectCommand.CommandText = query;
DataSet ds = new DataSet();
adapter.Fill(ds, "list");
return ds;
}
catch (Exception exp)
{
return null;
}
}

public void removeEntity(int id, string tableName)
{
try
{
deleteCommand.CommandText = "DELETE FROM tbl" + tableName + " ;";
deleteCommand.ExecuteNonQuery();
}
catch (Exception exp)
{
}
}

public bool execute(string query)
{
try
{
updateCommand.CommandText = query;
updateCommand.ExecuteNonQuery();
return true;
}
catch (Exception exp)
{
return false;
}
}


}

و توی صفحه programming.cs میخوام به بانک وصل بشم
کاری که من انجام دادم اینه:
private dataaccess dataaccess1 = new dataaccess();
private DataSet d1,d2;

dataaccess1.openConnection();
string Query = "INSERT INTO Tbl_Ostad (fldid,fldname) VALUES ('13','2')";
dataaccess1.execute(Query);
dataaccess1.connection.Close();ولی به بانک وصل نمیشه
ضمنا من از athunخود ویندوز به بانکم که sql server هست وصل می شم.لطفا دوستان راهنمایی کنن چون خیلی برام ضروریه

mortezawolf
یک شنبه 24 خرداد 1388, 13:52 عصر
دوستان اهل فن لطفا راهنمایی کنن

mortezawolf
یک شنبه 24 خرداد 1388, 23:23 عصر
نبوووووود؟