【发布时间】:2013-08-03 23:48:00
【问题描述】:
这个问题已经在整个网络上得到解决,我尝试了很多事情都没有成功。 SQL EXPRESS 服务设置为接受本地系统帐户,但问题仍然存在。
这是我的连接字符串:
<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;
-
是的,我意识到了这一点!非常感谢你的解释,这就是我喜欢听到的!有时你会错过一些简单的事情,然后你想弄清楚它就会出现在你面前!
-
当出现问题时,得到第二次关注总是好的。 ;-)
标签: sql-server asp.net-mvc connection-string