[英]Whats wrong with my connection string?
你好,請幫我解決這個連接字符串,甚至我的過程屬性也被附加了
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]
上面的代碼可能不是連接數據庫的最佳方式。
在您的 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();
打開連接con.Open();
試試這個,如果你使用的是本地機器
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");
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.