我正在尝试使用asp.net访问在线SQL Server数据库
我在代码中使用了以下行,
string connectionString = "Data Source=xx.xx.20x.82;Initial Catalog=mydbname;User ID=myusername;Password=mypass";
string insertSql = "INSERT INTO Users (FirstName, LastName, UserName, Password, Email, Address, Gender, Phone, Pincode) VALUES (@FirstName, @LastName, @UserName, @Password, @Email, @Address, @Gender, @Phone, @Pincode)";
//Create SQL connection
SqlConnection con = new SqlConnection(connectionString);
//Create SQL Command And Sql Parameters
SqlCommand cmd = new SqlCommand(insertSql);
cmd.Connection = con;
cmd.CommandType = CommandType.Text;
cmd.CommandText = insertSql;
SqlParameter firstName = new SqlParameter("@FirstName", SqlDbType.VarChar,50);
firstName.Value = txtFirstName.Text.ToString();
cmd.Parameters.Add(firstName);
SqlParameter lastName = new SqlParameter("@LastName", SqlDbType.VarChar,50);
lastName.Value = txtLastName.Text.ToString();
cmd.Parameters.Add(lastName);
SqlParameter Phone = new SqlParameter("@Phone", SqlDbType.VarChar, 12);
Phone.Value = txtPhone.Text.ToString();
cmd.Parameters.Add(Phone);
SqlParameter Pincode = new SqlParameter("@Pincode", SqlDbType.VarChar, 10);
Pincode.Value = txtPin.Text.ToString();
cmd.Parameters.Add(Pincode);
SqlParameter userName = new SqlParameter("@UserName", SqlDbType.VarChar, 10);
userName.Value = txtUserName.Text.ToString();
cmd.Parameters.Add(userName);
SqlParameter pwd = new SqlParameter("@Password", SqlDbType.VarChar, 10);
pwd.Value = txtPwd.Text.ToString();
cmd.Parameters.Add(pwd);
SqlParameter email = new SqlParameter("@Email", SqlDbType.VarChar, 50);
email.Value = txtEmailID.Text.ToString();
cmd.Parameters.Add(email);
SqlParameter address = new SqlParameter("@Address", SqlDbType.VarChar, 100);
address.Value = txtAdress.Text.ToString();
cmd.Parameters.Add(address);
SqlParameter gender = new SqlParameter("@Gender", SqlDbType.VarChar,10);
gender.Value = rdoGender.SelectedItem.ToString();
cmd.Parameters.Add(gender);
try
{
con.Open();
cmd.ExecuteNonQuery();
lblMsg.Text = "User Registration successful";
ClearControls(Page);
}
catch (SqlException ex)
{
string errorMessage = "Error in registring user";
errorMessage += ex.Message;
throw new Exception(errorMessage);
}
和我的web.config
文件就像这样:
<connectionStrings>
<add name="ConnectionString"
connectionString=" Server=whsql-v04.prod.mesa1.secureserver.net; Database=mydbname; User ID=myusername; Password=mypass; Trusted_Connection=False"
providerName="System.Data.SqlClient" />
<remove name="LocalSqlServer"/>
<add name="LocalSqlServer"
connectionString=" Server=whsql-v04.prod.mesa1.secureserver.net; Database=mydbname; User ID=myusername; Password=mypass; Trusted_Connection=False"
providerName="System.Data.SqlClient" />
</connectionStrings>
当我运行代码时出现错误...
我犯了什么错误?
请帮帮我......
错误讯息......
Server Error in '/UserRegistration' Application.
Error in registring userA network-related or instance-specific error occurred while establishing
a connection to SQL Server. The server was not found or was not accessible. Verify that the
instance name is correct and that SQL Server is configured to allow remote connections.
(provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)
Description: An unhandled exception occurred during the execution of the current web request.
Please review the stack trace for more information about the error and where it originated in the
code.
Exception Details: System.Exception: Error in registring userA network-related or instance-
specific error occurred while establishing a connection to SQL Server. The server was not found
or was not accessible. Verify that the instance name is correct and that SQL Server is configured
to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a
connection to SQL Server)
Source Error:
Line 80: string errorMessage = "Error in registring user";
Line 81: errorMessage += ex.Message;
Line 82: throw new Exception(errorMessage);
Line 83:
Line 84: }
Source File: e:\UserRegistration\Default.aspx.cs Line: 82
Stack Trace:
[Exception: Error in registring userA network-related or instance-specific error occurred while
establishing a connection to SQL Server. The server was not found or was not accessible. Verify
that the instance name is correct and that SQL Server is configured to allow remote connections.
(provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)]
_Default.btnSave_Click(Object sender, EventArgs e) in e:\UserRegistration\Default.aspx.cs:82
System.Web.UI.WebControls.Button.OnClick(EventArgs e) +111
System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +110
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String
eventArgument) +10
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument)
+13
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +36
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean
includeStagesAfterAsyncPoint) +1565
Version Information: Microsoft .NET Framework Version:2.0.50727.4927; ASP.NET
Version:2.0.50727.4927
答案 0 :(得分:0)
connectionString不正确,请使用
//Create SQL connection
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);