你好,请帮我解决这个连接字符串,甚至附上我的程序属性
try
{
conn = new SqlConnection("Server=RM-MOBL\MSSQLSERVER1;DataBase=master;Trusted_Connection=True;");
conn.Open();
SqlCommand cmd = new SqlCommand("dbo.new", conn);
cmd.CommandType = CommandType.StoredProcedure;
rdr = cmd.ExecuteReader();
Console.WriteLine(" connection success");
}
// I hope I have mentioned correct connection string but
// not able to execute my stored procedure
我正面临错误
![请在此处查看错误] [3]
答案 0 :(得分:0)
上述代码可能不是连接数据库的最佳方式。
在您的web.config中添加这些将连接到<configuration>
部分
<connectionStrings >
<add name="myConnectionString" connectionString="Server=ServerName;Database=DatabaseName;User ID=UserId;Password=Password;Trusted_Connection=False;"
providerName="System.Data.SqlClient"/>
</connectionStrings>
在您的C#文件中添加这些引用
using System.Data.SqlClient;
using System.Web.Configuration;
公共类内部添加此连接
SqlConnection con = new SqlConnection(WebConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString);
现在您可以通过con.Open();
答案 1 :(得分:0)
如果您使用的是本地计算机,请尝试此操作
SqlConnection con = new SqlConnection("Server=RANJITHM-OBL\MSSQLSERVER1;initial catalog=master;integrated security=true");
SqlCommand cmd = new SqlCommand("dbo.new", conn);
cmd.CommandType = CommandType.StoredProcedure;
if (con.State == ConnectionState.Closed)
con.Open();
rdr = cmd.ExecuteReader();
Console.WriteLine(" connection success");