配置ConnectionString以从SQL Server 2008中的不同域进行连接 [英] Configure ConnectionString for connect from different domain in SQL Server 2008
问题描述
我想从另一个域连接MS-SQL SERVER 2008的数据库。
MS-SQL Server 2008的ip是:192.168。 55.33
服务器名称:BDC\SQLEXPRESS
数据库名称:DMNG
登录:sa
密码:samsung001
我的App.config是:
I want to connect with MS-SQL SERVER 2008's Database from a different domain.
The ip of MS-SQL Server 2008 is: 192.168.55.33
Server Name: BDC\SQLEXPRESS
DataBase Name: DMNG
Login: sa
Password: samsung001
My App.config is:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<!-- User application and configured property settings go here.-->
<!-- Example: <add key="settingName" value="settingValue"/> -->
<add key="SqlConnectionString" value="Data Source=192.168.55.33\BDC\SQLEXPRESS;Initial Catalog=DMNG;UID=sa;Password=Support2010; Integrated Security=True;"/>
</appSettings>
</configuration>
我的SqlConnectoin.cs是:
My SqlConnectoin.cs is:
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.IO;
namespace DMNG_Management
{
public class SqlDBAccess
{
#region Variables
private string _connectionString;
private string _databaseName;
private string _serverName;
private string _userName;
private string _password;
public SqlConnection connection = new SqlConnection();
public SqlCommand command = new SqlCommand();
private SqlDataAdapter adapter = new SqlDataAdapter();
private SqlDataReader reader;
public DataSet dataSet = new DataSet();
private DataTable dataTable = new DataTable();
private SqlCommandBuilder commandBld = new SqlCommandBuilder();
public SqlTransaction transaction = null;
private Object obj;
#endregion
#region Properties
public string ConnectionString
{
set
{
_connectionString = value;
}
get
{
return _connectionString;
}
}
public string DatabaseName
{
set
{
_databaseName = value;
connection.ChangeDatabase(_databaseName);
}
get
{
return _databaseName;
}
}
public string ServerName
{
set
{
_serverName = value;
}
get
{
return _serverName;
}
}
public string UserName
{
set
{
_userName = value;
}
get
{
return _userName;
}
}
public string Password
{
set
{
_password = value;
}
get
{
return _password;
}
}
#endregion
#region Constructor
public SqlDBAccess()
{
_connectionString = ConfigurationSettings.AppSettings["Server"]; //"server=(local);database = db_IFRMS;persist Security Info=False;user=sa;password=@pwd99$02!;connect Timeout=5";
//DecriptString();
}
public SqlDBAccess(string conStr)
{
_connectionString = conStr;
//DecriptString();
}
#endregion
#region Security Methods
private void DecriptString()
{
string[] parts = _connectionString.Split(';');
string[] words = parts[0].Split('=');
_serverName = words[1].Trim();
words = parts[1].Split('=');
_databaseName = words[1].Trim();
words = parts[3].Split('=');
_userName = words[1].Trim();
words = parts[4].Split('=');
_password = words[1].Trim();
}
#endregion
#region Connection Method
public void OpenConnection()
{
try
{
if (!(connection.State.ToString() == "Open"))
{
connection.ConnectionString = _connectionString;
connection.Open();
}
}
catch (SqlException ex)
{
Log("Error in open connection", ex);
throw ex;
}
catch (Exception ex)
{
Log("Error in open connection", ex);
throw ex;
}
}
public void CloseConnection()
{
try
{
if (connection.State.ToString() == "Open")
connection.Close();
}
catch (SqlException ex)
{
Log("Error in close connection", ex);
throw new ApplicationException("Error in close connection", ex);
}
catch (Exception ex)
{
Log("Error in close connection", ex);
throw new ApplicationException("Error in close connection", ex);
}
}
#endregion
#region ExecuteNonQuery
public int ExecuteNonQuery(string sqlStmt)
{
OpenConnection();
int rowCount = 0;
try
{
transaction = connection.BeginTransaction();
command.CommandText = sqlStmt;
command.Connection = connection;
command.CommandType = CommandType.Text;
command.Transaction = transaction;
rowCount = command.ExecuteNonQuery();
transaction.Commit();
}
catch (SqlException ex)
{
if (transaction != null)
transaction.Rollback();
Log("Error in ExecuteQuery", ex);
throw new ApplicationException(ex.Message);
}
catch (Exception ex)
{
if (transaction != null)
transaction.Rollback();
Log("Error in ExecuteQuery", ex);
throw ex;
}
finally
{
CloseConnection();
}
return rowCount;
}
public int ExecuteNonQuery(string sqlStmt, bool IsStoredProcedure)
{
OpenConnection();
int rowCount = 0;
try
{
transaction = connection.BeginTransaction();
command.CommandText = sqlStmt;
command.Connection = connection;
if (IsStoredProcedure)
command.CommandType = CommandType.StoredProcedure;
else
command.CommandType = CommandType.Text;
command.Transaction = transaction;
rowCount = command.ExecuteNonQuery();
transaction.Commit();
}
catch (SqlException ex)
{
if (transaction != null)
transaction.Rollback();
throw new ApplicationException(ex.Message);
}
finally
{
CloseConnection();
command.Parameters.Clear();
}
return rowCount;
}
public int ExecuteMulNonQuery(string sqlStmt)
{
int rowCount = 0;
try
{
command.CommandText = sqlStmt;
command.Connection = connection;
command.CommandType = CommandType.Text;
rowCount = command.ExecuteNonQuery();
}
catch (SqlException ex)
{
throw new ApplicationException(ex.Message);
}
return rowCount;
}
#endregion
#region Execute Reader Methods
public SqlDataReader ExecuteReader(string sqlStmt)
{
try
{
OpenConnection();
command.CommandText = sqlStmt;
command.Connection = connection;
command.CommandType = CommandType.Text;
reader = command.ExecuteReader();
}
catch (SqlException ex)
{
throw new ApplicationException(ex.Message);
}
catch (Exception ex)
{
throw new ApplicationException(ex.Message);
}
return reader;
}
public SqlDataReader ExecuteReader(string sqlStmt, bool IsStoredProcedure)
{
OpenConnection();
try
{
command.CommandText = sqlStmt;
command.Connection = connection;
if (IsStoredProcedure)
command.CommandType = CommandType.StoredProcedure;
else
command.CommandType = CommandType.Text;
reader = command.ExecuteReader();
}
catch (SqlException ex)
{
throw new ApplicationException(ex.Message);
}
catch (Exception ex)
{
throw new ApplicationException(ex.Message);
}
return reader;
}
public Object ExecuteScalar(string sqlStmt)
{
OpenConnection();
try
{
command.CommandText = sqlStmt;
command.Connection = connection;
command.CommandType = CommandType.Text;
obj = command.ExecuteScalar();
}
catch (SqlException ex)
{
throw new ApplicationException(ex.Message);
}
finally
{
CloseConnection();
}
return obj;
}
#endregion Reader Methods
#region Adapter Methods
public DataTable Adapter(string sqlStmt)
{
OpenConnection();
try
{
command.CommandText = sqlStmt;
command.Connection = connection;
command.CommandType = CommandType.Text;
adapter.SelectCommand = command;
if (dataTable != null)
dataTable.Clear();
adapter.Fill(dataTable);
commandBld.DataAdapter = adapter;
}
catch (SqlException ex)
{
throw new ApplicationException(ex.Message);
}
finally
{
CloseConnection();
}
return dataTable;
}
public DataTable Adapter(string sqlStmt, bool IsStoredProcedure)
{
OpenConnection();
try
{
command.CommandText = sqlStmt;
command.Connection = connection;
if (IsStoredProcedure)
command.CommandType = CommandType.StoredProcedure;
else
command.CommandType = CommandType.Text;
adapter.SelectCommand = command;
if (dataTable != null)
dataTable.Clear();
adapter.Fill(dataTable);
commandBld.DataAdapter = adapter;
}
catch (SqlException ex)
{
throw new ApplicationException(ex.Message);
}
finally
{
CloseConnection();
command.Parameters.Clear();
}
return dataTable;
}
public void Adapter(string sqlStmt, string tableName, bool IsStoredProcedure)
{
OpenConnection();
try
{
command.CommandText = sqlStmt;
command.Connection = connection;
if (IsStoredProcedure)
command.CommandType = CommandType.StoredProcedure;
else
command.CommandType = CommandType.Text;
adapter.SelectCommand = command;
if (dataSet.Tables[tableName] != null)
dataSet.Tables[tableName].Clear();
adapter.Fill(dataSet, tableName);
commandBld.DataAdapter = adapter;
}
catch (SqlException ex)
{
throw new ApplicationException(ex.Message);
}
finally
{
CloseConnection();
command.Parameters.Clear();
}
}
public void Adapter(string sqlStmt, string tableName)
{
OpenConnection();
try
{
command.CommandText = sqlStmt;
command.Connection = connection;
command.CommandType = CommandType.Text;
adapter.SelectCommand = command;
if (dataSet.Tables[tableName] != null)
dataSet.Tables[tableName].Clear();
adapter.Fill(dataSet, tableName);
commandBld.DataAdapter = adapter;
}
catch (SqlException ex)
{
throw new ApplicationException(ex.Message);
}
finally
{
CloseConnection();
}
}
public void Adapter(string sqlStmt, DataTable table)
{
try
{
OpenConnection();
command.CommandText = sqlStmt;
command.Connection = connection;
command.CommandType = CommandType.Text;
adapter.SelectCommand = command;
adapter.Fill(table);
commandBld.DataAdapter = adapter;
}
catch (SqlException ex)
{
throw new ApplicationException(ex.Message);
}
catch (Exception ex)
{
throw new ApplicationException(ex.Message);
}
finally
{
CloseConnection();
}
}
public void Adapter(string sqlStmt, DataTable table, bool IsStoredProcedure)
{
OpenConnection();
try
{
command.CommandText = sqlStmt;
command.Connection = connection;
adapter.SelectCommand = command;
if (IsStoredProcedure)
command.CommandType = CommandType.StoredProcedure;
else
command.CommandType = CommandType.Text;
adapter.Fill(table);
commandBld.DataAdapter = adapter;
}
catch (SqlException ex)
{
throw new ApplicationException(ex.Message);
}
catch (Exception ex)
{
throw new ApplicationException(ex.Message);
}
finally
{
CloseConnection();
command.Parameters.Clear();
}
}
public void DataSetToDB(DataTable table)
{
OpenConnection();
try
{
adapter.Update(table);
dataSet.AcceptChanges();
}
catch (SqlException ex)
{
throw new ApplicationException(ex.Message);
}
finally
{
CloseConnection();
}
}
#endregion Adapter Methods
#region Error Log Methods
public static void Log(string Message)
{
Log(Message, null);
}
public static void Log(string Message, Exception Ex)
{
string fileName = Environment.CurrentDirectory + "\\" + "Error.log";
using (StreamWriter logFile = new StreamWriter(fileName, true))
{
logFile.WriteLine("{0}: {1}", DateTime.Now, Message);
if (Ex != null)
logFile.WriteLine(Ex.ToString());
logFile.Close();
}
}
#endregion Error Log Methods
}
}
现在,当我尝试连接数据库时,我收到System.Data.SqlClient.SqlError的异常:网络相关问题。
请帮我解决这个问题。
谢谢。
Now I am getting Exception for System.Data.SqlClient.SqlError: "A Network Related Problem" when I am trying to connect with my database.
Please help me to solve this problem.
Thank you.
推荐答案
02 !; connect Timeout = 5;
// DecriptString();
}
public SqlDBAccess( string conStr)
{
_connectionString = conStr;
/ / DecriptString();
}
#endregion
#region安全方法
private void DecriptString()
{
string [] parts = _connectionString.Split(' ;');
string [] words = parts [ 0 ]。分割(' ='跨度>);
_serverName = words[1].Trim();
words = parts[1].Split('=');
_databaseName = words[1].Trim();
words = parts[3].Split('=');
_userName = words[1].Trim();
words = parts[4].Split('=');
_password = words[1].Trim();
}
#endregion
#region Connection Method
public void OpenConnection()
{
try
{
if (!(connection.State.ToString() == \"Open\"))
{
connection.ConnectionString = _connectionString;
connection.Open();
}
}
catch (SqlException ex)
{
Log(\"Error in open connection\", ex);
throw ex;
}
catch (Exception ex)
{
Log(\"Error in open connection\", ex);
throw ex;
}
}
public void CloseConnection()
{
try
{
if (connection.State.ToString() == \"Open\")
connection.Close();
}
catch (SqlException ex)
{
Log(\"Error in close connection\", ex);
throw new ApplicationException(\"Error in close connection\", ex);
}
catch (Exception ex)
{
Log(\"Error in close connection\", ex);
throw new ApplicationException(\"Error in close connection\", ex);
}
}
#endregion
#region ExecuteNonQuery
public int ExecuteNonQuery(string sqlStmt)
{
OpenConnection();
int rowCount = 0;
try
{
transaction = connection.BeginTransaction();
command.CommandText = sqlStmt;
command.Connection = connection;
command.CommandType = CommandType.Text;
command.Transaction = transaction;
rowCount = command.ExecuteNonQuery();
transaction.Commit();
}
catch (SqlException ex)
{
if (transaction != null)
transaction.Rollback();
Log(\"Error in ExecuteQuery\", ex);
throw new ApplicationException(ex.Message);
}
catch (Exception ex)
{
if (transaction != null)
transaction.Rollback();
Log(\"Error in ExecuteQuery\", ex);
throw ex;
}
最后
{
CloseConnection();
}
return rowCount;
}
public int ExecuteNonQuery(string sqlStmt, bool IsStoredProcedure)
{
OpenConnection();
int rowCount = 0;
try
{
transaction = connection.BeginTransaction();
command.CommandText = sqlStmt;
command.Connection = connection;
if (IsStoredProcedure)
command.CommandType = CommandType.StoredProcedure;
else
command.CommandType = CommandType.Text;
command.Transaction = transaction;
rowCount = command.ExecuteNonQuery();
transaction.Commit();
}
catch (SqlException ex)
{
if (transaction != null)
transaction.Rollback();
throw new ApplicationException(ex.Message);
}
最后
{
CloseConnection();
command.Parameters.Clear();
}
return rowCount;
}
public int ExecuteMulNonQuery(string sqlStmt)
{
int rowCount = 0;
try
{
command.CommandText = sqlStmt;
command.Connection = connection;
command.CommandType = CommandType.Text;
rowCount = command.ExecuteNonQuery();
}
catch (SqlException ex)
{
throw new ApplicationException(ex.Message);
}
return rowCount;
}
#endregion
#region Execute Reader Methods
public SqlDataReader ExecuteReader(string sqlStmt)
{
try
{
OpenConnection();
command.CommandText = sqlStmt;
command.Connection = connection;
command.CommandType = CommandType.Text;
reader = command.ExecuteReader();
}
catch (SqlException ex)
{
throw new ApplicationException(ex.Message);
}
catch (Exception ex)
{
throw new ApplicationException(ex.Message);
}
return reader;
}
public SqlDataReader ExecuteReader(string sqlStmt, bool IsStoredProcedure)
{
OpenConnection();
try
{
command.CommandText = sqlStmt;
command.Connection = connection;
if (IsStoredProcedure)
command.CommandType = CommandType.StoredProcedure;
else
command.CommandType = CommandType.Text;
reader = command.ExecuteReader();
}
catch (SqlException ex)
{
throw new ApplicationException(ex.Message);
}
catch (Exception ex)
{
throw new ApplicationException(ex.Message);
}
return reader;
}
public Object ExecuteScalar(string sqlStmt)
{
OpenConnection();
try
{
command.CommandText = sqlStmt;
command.Connection = connection;
command.CommandType = CommandType.Text;
obj = command.ExecuteScalar();
}
catch (SqlException ex)
{
throw new ApplicationException(ex.Message);
}
最后
{
CloseConnection();
}
return obj;
}
#endregion Reader Methods
#region Adapter Methods
public DataTable Adapter(string sqlStmt)
{
OpenConnection();
try
{
command.CommandText = sqlStmt;
command.Connection = connection;
command.CommandType = CommandType.Text;
adapter.SelectCommand = command;
if (dataTable != null)
dataTable.Clear();
adapter.Fill(dataTable);
commandBld.DataAdapter = adapter;
}
catch (SqlException ex)
{
throw new ApplicationException(ex.Message);
}
最后
{
CloseConnection();
}
return dataTable;
}
public DataTable Adapter(string sqlStmt, bool IsStoredProcedure)
{
OpenConnection();
try
{
command.CommandText = sqlStmt;
command.Connection = connection;
if (IsStoredProcedure)
command.CommandType = CommandType.StoredProcedure;
else
command.CommandType = CommandType.Text;
adapter.SelectCommand = command;
if (dataTable != null)
dataTable.Clear();
adapter.Fill(dataTable);
commandBld.DataAdapter = adapter;
}
catch (SqlException ex)
{
throw new ApplicationException(ex.Message);
}
最后
{
CloseConnection();
command.Parameters.Clear();
}
return dataTable;
}
public void Adapter(string sqlStmt, string tableName, bool IsStoredProcedure)
{
OpenConnection();
try
{
command.CommandText = sqlStmt;
command.Connection = connection;
if (IsStoredProcedure)
command.CommandType = CommandType.StoredProcedure;
else
command.CommandType = CommandType.Text;
adapter.SelectCommand = command;
if (dataSet.Tables[tableName] != null)
dataSet.Tables[tableName].Clear();
adapter.Fill(dataSet, tableName);
commandBld.DataAdapter = adapter;
}
catch (SqlException ex)
{
throw new ApplicationException(ex.Message);
}
最后
{
CloseConnection();
command.Parameters.Clear();
}
}
public void Adapter(string sqlStmt, string tableName)
{
OpenConnection();
try
{
command.CommandText = sqlStmt;
command.Connection = connection;
command.CommandType = CommandType.Text;
adapter.SelectCommand = command;
if (dataSet.Tables[tableName] != null)
dataSet.Tables[tableName].Clear();
adapter.Fill(dataSet, tableName);
commandBld.DataAdapter = adapter;
}
catch (SqlException ex)
{
throw new ApplicationException(ex.Message);
}
最后
{
CloseConnection();
}
}
public void Adapter(string sqlStmt, DataTable table)
{
try
{
OpenConnection();
command.CommandText = sqlStmt;
command.Connection = connection;
command.CommandType = CommandType.Text;
adapter.SelectCommand = command;
adapter.Fill(table);
commandBld.DataAdapter = adapter;
}
catch (SqlException ex)
{
throw new ApplicationException(ex.Message);
}
catch (Exception ex)
{
throw new ApplicationException(ex.Message);
}
finally
{
CloseConnection();
}
}
public void Adapter(string sqlStmt, DataTable table, bool IsStoredProcedure)
{
OpenConnection();
try
{
command.CommandText = sqlStmt;
command.Connection = connection;
adapter.SelectCommand = command;
if (IsStoredProcedure)
command.CommandType = CommandType.StoredProcedure;
else
command.CommandType = CommandType.Text;
adapter.Fill(table);
commandBld.DataAdapter = adapter;
}
catch (SqlException ex)
{
throw new ApplicationException(ex.Message);
}
catch (Exception ex)
{
throw new ApplicationException(ex.Message);
}
finally
{
CloseConnection();
command.Parameters.Clear();
}
}
public void DataSetToDB(DataTable table)
{
OpenConnection();
try
{
adapter.Update(table);
dataSet.AcceptChanges();
}
catch (SqlException ex)
{
throw new ApplicationException(ex.Message);
}
最后
{
CloseConnection();
}
}
#endregion Adapter Methods
#region Error Log Methods
public static void Log(string Message)
{
Log(Message, null);
}
public static void Log(string Message, Exception Ex)
{
string fileName = Environment.CurrentDirectory + \"\\\" + \"Error.log\";
using (StreamWriter logFile = new StreamWriter(fileName, true))
{
logFile.WriteLine(\"{0}: {1}\", DateTime.Now, Message);
if (Ex != null)
logFile.WriteLine(Ex.ToString());
logFile.Close();
}
}
#endregion Error Log Methods
}
}
02!;connect Timeout=5"; //DecriptString(); } public SqlDBAccess(string conStr) { _connectionString = conStr; //DecriptString(); } #endregion #region Security Methods private void DecriptString() { string[] parts = _connectionString.Split(';'); string[] words = parts[0].Split('='); _serverName = words[1].Trim(); words = parts[1].Split('='); _databaseName = words[1].Trim(); words = parts[3].Split('='); _userName = words[1].Trim(); words = parts[4].Split('='); _password = words[1].Trim(); } #endregion #region Connection Method public void OpenConnection() { try { if (!(connection.State.ToString() == "Open")) { connection.ConnectionString = _connectionString; connection.Open(); } } catch (SqlException ex) { Log("Error in open connection", ex); throw ex; } catch (Exception ex) { Log("Error in open connection", ex); throw ex; } } public void CloseConnection() { try { if (connection.State.ToString() == "Open") connection.Close(); } catch (SqlException ex) { Log("Error in close connection", ex); throw new ApplicationException("Error in close connection", ex); } catch (Exception ex) { Log("Error in close connection", ex); throw new ApplicationException("Error in close connection", ex); } } #endregion #region ExecuteNonQuery public int ExecuteNonQuery(string sqlStmt) { OpenConnection(); int rowCount = 0; try { transaction = connection.BeginTransaction(); command.CommandText = sqlStmt; command.Connection = connection; command.CommandType = CommandType.Text; command.Transaction = transaction; rowCount = command.ExecuteNonQuery(); transaction.Commit(); } catch (SqlException ex) { if (transaction != null) transaction.Rollback(); Log("Error in ExecuteQuery", ex); throw new ApplicationException(ex.Message); } catch (Exception ex) { if (transaction != null) transaction.Rollback(); Log("Error in ExecuteQuery", ex); throw ex; } finally { CloseConnection(); } return rowCount; } public int ExecuteNonQuery(string sqlStmt, bool IsStoredProcedure) { OpenConnection(); int rowCount = 0; try { transaction = connection.BeginTransaction(); command.CommandText = sqlStmt; command.Connection = connection; if (IsStoredProcedure) command.CommandType = CommandType.StoredProcedure; else command.CommandType = CommandType.Text; command.Transaction = transaction; rowCount = command.ExecuteNonQuery(); transaction.Commit(); } catch (SqlException ex) { if (transaction != null) transaction.Rollback(); throw new ApplicationException(ex.Message); } finally { CloseConnection(); command.Parameters.Clear(); } return rowCount; } public int ExecuteMulNonQuery(string sqlStmt) { int rowCount = 0; try { command.CommandText = sqlStmt; command.Connection = connection; command.CommandType = CommandType.Text; rowCount = command.ExecuteNonQuery(); } catch (SqlException ex) { throw new ApplicationException(ex.Message); } return rowCount; } #endregion #region Execute Reader Methods public SqlDataReader ExecuteReader(string sqlStmt) { try { OpenConnection(); command.CommandText = sqlStmt; command.Connection = connection; command.CommandType = CommandType.Text; reader = command.ExecuteReader(); } catch (SqlException ex) { throw new ApplicationException(ex.Message); } catch (Exception ex) { throw new ApplicationException(ex.Message); } return reader; } public SqlDataReader ExecuteReader(string sqlStmt, bool IsStoredProcedure) { OpenConnection(); try { command.CommandText = sqlStmt; command.Connection = connection; if (IsStoredProcedure) command.CommandType = CommandType.StoredProcedure; else command.CommandType = CommandType.Text; reader = command.ExecuteReader(); } catch (SqlException ex) { throw new ApplicationException(ex.Message); } catch (Exception ex) { throw new ApplicationException(ex.Message); } return reader; } public Object ExecuteScalar(string sqlStmt) { OpenConnection(); try { command.CommandText = sqlStmt; command.Connection = connection; command.CommandType = CommandType.Text; obj = command.ExecuteScalar(); } catch (SqlException ex) { throw new ApplicationException(ex.Message); } finally { CloseConnection(); } return obj; } #endregion Reader Methods #region Adapter Methods public DataTable Adapter(string sqlStmt) { OpenConnection(); try { command.CommandText = sqlStmt; command.Connection = connection; command.CommandType = CommandType.Text; adapter.SelectCommand = command; if (dataTable != null) dataTable.Clear(); adapter.Fill(dataTable); commandBld.DataAdapter = adapter; } catch (SqlException ex) { throw new ApplicationException(ex.Message); } finally { CloseConnection(); } return dataTable; } public DataTable Adapter(string sqlStmt, bool IsStoredProcedure) { OpenConnection(); try { command.CommandText = sqlStmt; command.Connection = connection; if (IsStoredProcedure) command.CommandType = CommandType.StoredProcedure; else command.CommandType = CommandType.Text; adapter.SelectCommand = command; if (dataTable != null) dataTable.Clear(); adapter.Fill(dataTable); commandBld.DataAdapter = adapter; } catch (SqlException ex) { throw new ApplicationException(ex.Message); } finally { CloseConnection(); command.Parameters.Clear(); } return dataTable; } public void Adapter(string sqlStmt, string tableName, bool IsStoredProcedure) { OpenConnection(); try { command.CommandText = sqlStmt; command.Connection = connection; if (IsStoredProcedure) command.CommandType = CommandType.StoredProcedure; else command.CommandType = CommandType.Text; adapter.SelectCommand = command; if (dataSet.Tables[tableName] != null) dataSet.Tables[tableName].Clear(); adapter.Fill(dataSet, tableName); commandBld.DataAdapter = adapter; } catch (SqlException ex) { throw new ApplicationException(ex.Message); } finally { CloseConnection(); command.Parameters.Clear(); } } public void Adapter(string sqlStmt, string tableName) { OpenConnection(); try { command.CommandText = sqlStmt; command.Connection = connection; command.CommandType = CommandType.Text; adapter.SelectCommand = command; if (dataSet.Tables[tableName] != null) dataSet.Tables[tableName].Clear(); adapter.Fill(dataSet, tableName); commandBld.DataAdapter = adapter; } catch (SqlException ex) { throw new ApplicationException(ex.Message); } finally { CloseConnection(); } } public void Adapter(string sqlStmt, DataTable table) { try { OpenConnection(); command.CommandText = sqlStmt; command.Connection = connection; command.CommandType = CommandType.Text; adapter.SelectCommand = command; adapter.Fill(table); commandBld.DataAdapter = adapter; } catch (SqlException ex) { throw new ApplicationException(ex.Message); } catch (Exception ex) { throw new ApplicationException(ex.Message); } finally { CloseConnection(); } } public void Adapter(string sqlStmt, DataTable table, bool IsStoredProcedure) { OpenConnection(); try { command.CommandText = sqlStmt; command.Connection = connection; adapter.SelectCommand = command; if (IsStoredProcedure) command.CommandType = CommandType.StoredProcedure; else command.CommandType = CommandType.Text; adapter.Fill(table); commandBld.DataAdapter = adapter; } catch (SqlException ex) { throw new ApplicationException(ex.Message); } catch (Exception ex) { throw new ApplicationException(ex.Message); } finally { CloseConnection(); command.Parameters.Clear(); } } public void DataSetToDB(DataTable table) { OpenConnection(); try { adapter.Update(table); dataSet.AcceptChanges(); } catch (SqlException ex) { throw new ApplicationException(ex.Message); } finally { CloseConnection(); } } #endregion Adapter Methods #region Error Log Methods public static void Log(string Message) { Log(Message, null); } public static void Log(string Message, Exception Ex) { string fileName = Environment.CurrentDirectory + "\\" + "Error.log"; using (StreamWriter logFile = new StreamWriter(fileName, true)) { logFile.WriteLine("{0}: {1}", DateTime.Now, Message); if (Ex != null) logFile.WriteLine(Ex.ToString()); logFile.Close(); } } #endregion Error Log Methods } }
Now I am getting Exception for System.Data.SqlClient.SqlError: \"A Network Related Problem\" when I am trying to connect with my database.
Please help me to solve this problem.
Thank you.
Now I am getting Exception for System.Data.SqlClient.SqlError: "A Network Related Problem" when I am trying to connect with my database.
Please help me to solve this problem.
Thank you.
Refer to this link..
Hope it may help..
http://blogs.msdn.com/b/sql_protocols/archive/2008/04/30/steps-to-troubleshoot-connectivity-issues.aspx
Refer to this link..
Hope it may help..
http://blogs.msdn.com/b/sql_protocols/archive/2008/04/30/steps-to-troubleshoot-connectivity-issues.aspx
这篇关于配置ConnectionString以从SQL Server 2008中的不同域进行连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!