SqlConnection 的连接字符串 属性 未初始化
Connection string property of SqlConnection is not initialized
我已经搜索过错误
the connection string property has not been initialized.
在 Google 以及 Stack Overflow 上,但找不到解决方案。我创建了一个数据库 class 用于与数据库交互,所有相关代码都写在这个文件中。问题是相同的代码在其他页面上运行良好,但它在名为 "addevent.aspx" 的页面上不起作用 我不明白为什么它不正确 运行 的原因。
这是我在 database.cs 文件
中创建的方法
public void CreateConnection()
{
var ConfiguredString = ConfigurationManager.ConnectionStrings[ConnectionString].ConnectionString;
obj_sqlconnection = new SqlConnection(ConfiguredString);
}
//This property will set the connection string for database
public string ConnectionString
{
get
{ //if _connectionString is already created or set, only then it will return the value of _connectionString
if (_connectionString != string.Empty && _connectionString != "" && _connectionString != null)
return _connectionString;
else
return string.Empty;
}
// When you want to set the connection string set block is called.
set
{ // this line sets the connection string to the _connectionString data member for the first time.
if (_connectionString == string.Empty || _connectionString == "" || _connectionString == null)
_connectionString = value;
}
}
// Open database connection.
public void OpenConnection()
{
obj_sqlconnection.Open();
}
// Close database connection.
public void CloseConnection()
{
obj_sqlconnection.Close();
obj_sqlconnection.Dispose();
}
public SqlConnection GetCurrentConnection
{
get { return obj_sqlconnection; }
set { obj_sqlconnection = value; }
}
我根本不明白这个错误的逻辑和它的发生。打开连接时出现此错误
如何调用这些方法,我已经在 AddEvent 方法外创建了一个 database.cs class 的对象,对象名称为 mydb
public int AddEvent(string _title, string _description, string _place, int _eventTypeID, string _startingTime, string _endingTime, string _startingDate, string _endingDate, string _creatorID, string _picture)
{
string[] blacklist = { _title, _description, _place, _picture };
if (Jvalidate.FilterBlackLIstKeywords(blacklist))
{
int eventid = Convert.ToInt32(mydb.GetLastValueByColumnName("event_id", "tbl_events"));
int rowsaffected = 0;
mydb.CreateConnection();
mydb.InitializeSQLCommandObject(mydb.GetCurrentConnection, "spAddEvent", true);
mydb.obj_sqlcommand.Parameters.AddWithValue("@eventID", eventid + 1);
mydb.obj_sqlcommand.Parameters.AddWithValue("@title", _title);
mydb.obj_sqlcommand.Parameters.AddWithValue("@description", _description);
mydb.obj_sqlcommand.Parameters.AddWithValue("@place", _place);
mydb.obj_sqlcommand.Parameters.AddWithValue("@eventType", _eventTypeID);
mydb.obj_sqlcommand.Parameters.AddWithValue("@startingTime", _startingTime);
mydb.obj_sqlcommand.Parameters.AddWithValue("@endingTime", _endingTime);
mydb.obj_sqlcommand.Parameters.AddWithValue("@startDate", _startingDate);
mydb.obj_sqlcommand.Parameters.AddWithValue("@endDate", _endingDate);
mydb.obj_sqlcommand.Parameters.AddWithValue("@schoolID", SchoolID);
mydb.obj_sqlcommand.Parameters.AddWithValue("@eventCreatorID", _creatorID);
mydb.obj_sqlcommand.Parameters.AddWithValue("@eventPicture", _picture);
try
{
//mydb.obj_sqlconnection.ConnectionString = ConfigurationManager.ConnectionStrings["cesConnectionString"].ToString();
mydb.OpenConnection();
rowsaffected = mydb.obj_sqlcommand.ExecuteNonQuery();
}
finally
{
mydb.CloseConnection();
mydb.obj_sqlcommand.Dispose();
}
return rowsaffected;
}
return 0;
}
这个解决方案太复杂了...这将解决您的理解问题和不必要的代码行:
解决方案:
namespace Whosebug
{
public static class Solution
{
static readonly string _connectionStringName =
@"mainConnectionStringName";
static readonly string _connectionString =
_connectionStringName.getConnectionString();
// string extended method like .ToLower() or .Trim()
public static string getConnectionString(
this string connectionStringName)
{
return
System.
Configuration.
ConfigurationManager.
ConnectionStrings[connectionStringName].
ConnectionString;
}
public static object SqlExecute(
string connectionStringName,
string storedProcedureName,
System
.Collections
.Generic
.Dictionary<string, object> parameters,
bool isScalar)
{
object result = null;
using (System
.Data
.SqlClient
.SqlConnection connection =
new System.Data.SqlClient.SqlConnection(
string.IsNullOrWhiteSpace(connectionStringName)
? _connectionString
: connectionStringName.getConnectionString()))
if (connection != null)
using (System.Data.SqlClient.SqlCommand command =
new System.Data.SqlClient.SqlCommand()
{
CommandText = storedProcedureName,
CommandType = System
.Data
.CommandType
.StoredProcedure,
Connection = connection
})
if (command != null)
{
if (parameters != null)
foreach (System
.Collections
.Generic
.KeyValuePair<string, object>
pair in parameters)
command.Parameters.AddWithValue(
pair.Key, pair.Value);
command.Connection.Open();
result = isScalar
? command.ExecuteScalar()
: command.ExecuteNonQuery();
if (command.Connection.State ==
System.Data.ConnectionState.Open)
command.Connection.Close();
}
return result;
}
}
}
用法:
namespace SomeNamespace
{
public sealed class SomeClass
{
public int Example()
{
return (int)Whosebug
.Solution
.SqlExecute(
@"anyConnectionStringName", // or null for main connection string
@"anyStoredProcedureName",
new System
.Collections
.Generic
.Dictionary<string, object>()
{
{ @"field0", "value" },
{ @"field1", -1.5 },
{ @"field2", System.DateTime.Now },
{ @"field3", 3.5 },
{ @"field4", 7 },
},
false // for ExecuteNonQuery or true for ExecuteScalar
);
}
}
}
我已经搜索过错误
the connection string property has not been initialized.
在 Google 以及 Stack Overflow 上,但找不到解决方案。我创建了一个数据库 class 用于与数据库交互,所有相关代码都写在这个文件中。问题是相同的代码在其他页面上运行良好,但它在名为 "addevent.aspx" 的页面上不起作用 我不明白为什么它不正确 运行 的原因。
这是我在 database.cs 文件
中创建的方法public void CreateConnection()
{
var ConfiguredString = ConfigurationManager.ConnectionStrings[ConnectionString].ConnectionString;
obj_sqlconnection = new SqlConnection(ConfiguredString);
}
//This property will set the connection string for database
public string ConnectionString
{
get
{ //if _connectionString is already created or set, only then it will return the value of _connectionString
if (_connectionString != string.Empty && _connectionString != "" && _connectionString != null)
return _connectionString;
else
return string.Empty;
}
// When you want to set the connection string set block is called.
set
{ // this line sets the connection string to the _connectionString data member for the first time.
if (_connectionString == string.Empty || _connectionString == "" || _connectionString == null)
_connectionString = value;
}
}
// Open database connection.
public void OpenConnection()
{
obj_sqlconnection.Open();
}
// Close database connection.
public void CloseConnection()
{
obj_sqlconnection.Close();
obj_sqlconnection.Dispose();
}
public SqlConnection GetCurrentConnection
{
get { return obj_sqlconnection; }
set { obj_sqlconnection = value; }
}
我根本不明白这个错误的逻辑和它的发生。打开连接时出现此错误
如何调用这些方法,我已经在 AddEvent 方法外创建了一个 database.cs class 的对象,对象名称为 mydb
public int AddEvent(string _title, string _description, string _place, int _eventTypeID, string _startingTime, string _endingTime, string _startingDate, string _endingDate, string _creatorID, string _picture)
{
string[] blacklist = { _title, _description, _place, _picture };
if (Jvalidate.FilterBlackLIstKeywords(blacklist))
{
int eventid = Convert.ToInt32(mydb.GetLastValueByColumnName("event_id", "tbl_events"));
int rowsaffected = 0;
mydb.CreateConnection();
mydb.InitializeSQLCommandObject(mydb.GetCurrentConnection, "spAddEvent", true);
mydb.obj_sqlcommand.Parameters.AddWithValue("@eventID", eventid + 1);
mydb.obj_sqlcommand.Parameters.AddWithValue("@title", _title);
mydb.obj_sqlcommand.Parameters.AddWithValue("@description", _description);
mydb.obj_sqlcommand.Parameters.AddWithValue("@place", _place);
mydb.obj_sqlcommand.Parameters.AddWithValue("@eventType", _eventTypeID);
mydb.obj_sqlcommand.Parameters.AddWithValue("@startingTime", _startingTime);
mydb.obj_sqlcommand.Parameters.AddWithValue("@endingTime", _endingTime);
mydb.obj_sqlcommand.Parameters.AddWithValue("@startDate", _startingDate);
mydb.obj_sqlcommand.Parameters.AddWithValue("@endDate", _endingDate);
mydb.obj_sqlcommand.Parameters.AddWithValue("@schoolID", SchoolID);
mydb.obj_sqlcommand.Parameters.AddWithValue("@eventCreatorID", _creatorID);
mydb.obj_sqlcommand.Parameters.AddWithValue("@eventPicture", _picture);
try
{
//mydb.obj_sqlconnection.ConnectionString = ConfigurationManager.ConnectionStrings["cesConnectionString"].ToString();
mydb.OpenConnection();
rowsaffected = mydb.obj_sqlcommand.ExecuteNonQuery();
}
finally
{
mydb.CloseConnection();
mydb.obj_sqlcommand.Dispose();
}
return rowsaffected;
}
return 0;
}
这个解决方案太复杂了...这将解决您的理解问题和不必要的代码行:
解决方案:
namespace Whosebug
{
public static class Solution
{
static readonly string _connectionStringName =
@"mainConnectionStringName";
static readonly string _connectionString =
_connectionStringName.getConnectionString();
// string extended method like .ToLower() or .Trim()
public static string getConnectionString(
this string connectionStringName)
{
return
System.
Configuration.
ConfigurationManager.
ConnectionStrings[connectionStringName].
ConnectionString;
}
public static object SqlExecute(
string connectionStringName,
string storedProcedureName,
System
.Collections
.Generic
.Dictionary<string, object> parameters,
bool isScalar)
{
object result = null;
using (System
.Data
.SqlClient
.SqlConnection connection =
new System.Data.SqlClient.SqlConnection(
string.IsNullOrWhiteSpace(connectionStringName)
? _connectionString
: connectionStringName.getConnectionString()))
if (connection != null)
using (System.Data.SqlClient.SqlCommand command =
new System.Data.SqlClient.SqlCommand()
{
CommandText = storedProcedureName,
CommandType = System
.Data
.CommandType
.StoredProcedure,
Connection = connection
})
if (command != null)
{
if (parameters != null)
foreach (System
.Collections
.Generic
.KeyValuePair<string, object>
pair in parameters)
command.Parameters.AddWithValue(
pair.Key, pair.Value);
command.Connection.Open();
result = isScalar
? command.ExecuteScalar()
: command.ExecuteNonQuery();
if (command.Connection.State ==
System.Data.ConnectionState.Open)
command.Connection.Close();
}
return result;
}
}
}
用法:
namespace SomeNamespace
{
public sealed class SomeClass
{
public int Example()
{
return (int)Whosebug
.Solution
.SqlExecute(
@"anyConnectionStringName", // or null for main connection string
@"anyStoredProcedureName",
new System
.Collections
.Generic
.Dictionary<string, object>()
{
{ @"field0", "value" },
{ @"field1", -1.5 },
{ @"field2", System.DateTime.Now },
{ @"field3", 3.5 },
{ @"field4", 7 },
},
false // for ExecuteNonQuery or true for ExecuteScalar
);
}
}
}