这个问题已经全部在网上解决,我尝试了很多事情没有成功。 SQL EXPRESS服务设置为接受本地系统帐户,但问题仍然存在。连接属性尚未初始化错误(的ExecuteNonQuery)
这是我的连接字符串:
<add name="PhoneTemplateChange" providerName="System.Data.SqlClient" connectionString="Data Source=.\SQLEXPRESS;Database=PhoneTemplateChange;Integrated Security=SSPI" />
我创建了一个类的构造函数做数据库操作我有
_connectionString = System.Web.Configuration.WebConfigurationManager.ConnectionStrings["PhoneTemplateChange"].ConnectionString;
,并在这个类中的方法将数据插入
public void AddNewChangeOrder(int operation, int targetExt)
{
using (SqlConnection con = new SqlConnection(_connectionString))
{
string sql = "INSERT into [dbo].ChangeOrder (operation, targetExt, dtrequested) VALUES (@operation, @targetExt, @dtrequested)";
using (SqlCommand cmd = new SqlCommand(sql))
{
try
{
cmd.Parameters.AddWithValue("@operation", operation);
cmd.Parameters.AddWithValue("@targetExt", targetExt);
cmd.Parameters.AddWithValue("dtrequested", DateTime.Now);
//con.CreateCommand();
con.Open();
//cmd.InitializeLifetimeService();
int rows = cmd.ExecuteNonQuery();
con.Close();
}
catch (SqlException e)
{
throw new Exception(e.Message);
}
}
}
我打了周围的连接字符串,尝试所有不同的建议,也上述方法中的注释代码是我试图解决问题的方法。仍然没有运气!
我也改变了连接字符串,我得到两个不同的异常这样
Database=PhoneTemplateChange
上面给出的标题除外。 及以下给出了异常“可以通过登录无法打开数据库PhoneTemplatechange.mdf请登录失败,用户MYDOMAIN \名为myUsername'”
Database=PhoneTemplateChange.mdf
任何想法?
问自己一个问题,你上哪儿去分配你的应用程序的任何连接?您已声明但您的命令不知道要使用哪个连接。试试这个** SqlCommand cmd = new SqlCommand(sql,con)**,请不要拿我的话冒犯,但找到理由会帮助你。或者,你可以告诉你的命令使用此连接这种方式也** cmd.Connection = CON; ** –
是的,我意识到了这一点!并再次感谢解释,这就是我喜欢听到的!有时候你会错过简单的事情,而且你试图想出它,它就会在你面前! – laitha0
当遇到问题时,始终保持良好的态度。 ;-) – David