如何建立数据库(sql)连接来使用c#接收来自textfields的数据?

时间:2023-01-07 18:54:48

I want to establish the database connection for receiving the data from the Textfields and store that data in database records. For that, up to now I have tried:

我希望建立数据库连接,以便从Textfields接收数据,并将数据存储在数据库记录中。为此,我一直在努力:

I have created the .mdf database files and in that I have created the table with name as Table1 and I have placed the two textfields and submit button, with the following code : data.aspx

我创建了.mdf数据库文件,并在其中创建了名为Table1的表,并放置了两个textfields和submit按钮,代码如下:data.aspx

<b>Username:<asp:TextBox ID="TextBox1" runat="server" BackColor="AliceBlue">
</asp:TextBox><br/>
<b>Lastname:<asp:TextBox ID="TextBox2" runat="server" BackColor="AliceBlue">
</asp:TextBox><br/>
<asp:Button ID="Button1" runat="server" Text="Submit" OnClick="SubmitBtn_Click"/>

and the code file is as follows: data.aspx.cs

代码文件如下:data.aspx.cs

using System.Web.Configuration;
using System.Data.SqlClient;
protected void SubmitBtn_Click(object sender, EventArgs e)
{
    string connectionStrings = "Data Source=|SQLEXPRESS;Integrated                       
   Security=True; Connect Timeout=30;User Instance=True;";

    using (SqlConnection sqlConnection = new SqlConnection(connectionStrings))
{
    string insertStatement = "INSERT INTO Table1(column1,column2) 
                              VALUES (@col1, @col2)";
    SqlCommand sqlCommand = new SqlCommand(insertStatement, sqlConnection);
    sqlCommand.Parameters.AddWithValue("@col1", TextBox1.Text);
    sqlCommand.Parameters.AddWithValue("@col2", TextBox2.Text);
    sqlConnection.Open();
    sqlCommand.ExecuteNonQuery();
    try
    {
        sqlConnection.Open();
        sqlCommand.ExecuteNonQuery();

    }
    finally
    {
        sqlConnection.Close();
    }
}


}

and I have also the configuration files as follows to establish connection the code for that is as follows: web.config

我还有如下的配置文件来建立连接代码如下所示:web.config

<?xml version="1.0"?>
<configuration>
<system.web>
    <compilation debug="true" targetFramework="4.0" />

</system.web>
</configuration>

but for that code I am getting this error after clicking the submit button:

但是对于那个代码,我在点击提交按钮后会得到这个错误:

A 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)

在建立到SQL Server的连接时发生与网络相关或特定于实例的错误。没有找到或无法访问服务器。验证实例名是否正确,并将SQL Server配置为允许远程连接。(提供程序:命名管道提供程序,错误:40 -无法打开到SQL Server的连接)

and also the exception:

还有例外:

System.Data.SqlClient.SqlException: A 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)

System.Data.SqlClient。SqlException:在建立到SQL Server的连接时发生与网络相关或特定于实例的错误。没有找到或无法访问服务器。验证实例名是否正确,并将SQL Server配置为允许远程连接。(提供程序:命名管道提供程序,错误:40 -无法打开到SQL Server的连接)

Can any one help me?

有人能帮我吗?

2 个解决方案

#1


3  

Check if you sql server instance is running. Goto Start->SQL Server Configuration Manager
And check if your sql server instance has been started.

检查sql server实例是否正在运行。Goto Start->SQL Server配置管理器,检查SQL Server实例是否已经启动。

Or try changing your connection string to

或者尝试将连接字符串改为

string connectionStrings = "Data Source=.\SQLEXPRESS;
 Initial Catalog=databaseName Integrated Security=True;";

#2


0  

The exception is thrown because your SQL Server instance is not actually reachable, or your SQL Server instance is not started, or there's a firewall blocking the request for the default 1433 port, to reach remotely the SQL Server instance.

抛出异常是因为您的SQL Server实例实际上不可访问,或者SQL Server实例没有启动,或者有防火墙阻止对默认1433端口的请求,以远程访问SQL Server实例。

Read the documentation page: http://msdn.microsoft.com/en-us/library/ms177440.aspx

阅读文档页面:http://msdn.microsoft.com/en-us/library/ms177440.aspx

Remember to check your connection string. The connection string you wrote:

记得检查连接字符串。您所写的连接字符串:

"Data Source=|SQLEXPRESS;Integrated                       
   Security=True; Connect Timeout=30;User Instance=True;"

I think it's not correct. If you're connecting to your local 2nd SQL instance, named SQLEXPRESS, this is the correct connection string:

我认为这是不对的。如果要连接到本地的第二个SQL实例SQLEXPRESS,这就是正确的连接字符串:

"Data Source=.\SQLEXPRESS;Integrated                       
   Security=True; Connect Timeout=30;User Instance=True;"

Furthermore, you're using the setting Integrated Security=True;. This means that you're trying to connect using your Windows credentials. Are you sure you have the rights to connect with your Windows credentials? If not, remove this setting and add your username and password, like in this connection string:

此外,您正在使用设置集成安全性=True;。这意味着您正在尝试使用Windows凭据进行连接。您确定您拥有连接Windows凭据的权限吗?如果没有,请删除此设置并添加您的用户名和密码,如在此连接字符串中:

"Data Source=.\SQLEXPRESS;User Id=sa; Password=sa123; Connect Timeout=30;"

Read this interesting question about the use of Integrated Security setting.

阅读这个关于集成安全设置使用的有趣问题。

Also, your SQLEXPRESS instance seems to be a user instance. Is this instance enabled?

而且,您的SQLEXPRESS实例似乎是一个用户实例。这是启用实例吗?

#1


3  

Check if you sql server instance is running. Goto Start->SQL Server Configuration Manager
And check if your sql server instance has been started.

检查sql server实例是否正在运行。Goto Start->SQL Server配置管理器,检查SQL Server实例是否已经启动。

Or try changing your connection string to

或者尝试将连接字符串改为

string connectionStrings = "Data Source=.\SQLEXPRESS;
 Initial Catalog=databaseName Integrated Security=True;";

#2


0  

The exception is thrown because your SQL Server instance is not actually reachable, or your SQL Server instance is not started, or there's a firewall blocking the request for the default 1433 port, to reach remotely the SQL Server instance.

抛出异常是因为您的SQL Server实例实际上不可访问,或者SQL Server实例没有启动,或者有防火墙阻止对默认1433端口的请求,以远程访问SQL Server实例。

Read the documentation page: http://msdn.microsoft.com/en-us/library/ms177440.aspx

阅读文档页面:http://msdn.microsoft.com/en-us/library/ms177440.aspx

Remember to check your connection string. The connection string you wrote:

记得检查连接字符串。您所写的连接字符串:

"Data Source=|SQLEXPRESS;Integrated                       
   Security=True; Connect Timeout=30;User Instance=True;"

I think it's not correct. If you're connecting to your local 2nd SQL instance, named SQLEXPRESS, this is the correct connection string:

我认为这是不对的。如果要连接到本地的第二个SQL实例SQLEXPRESS,这就是正确的连接字符串:

"Data Source=.\SQLEXPRESS;Integrated                       
   Security=True; Connect Timeout=30;User Instance=True;"

Furthermore, you're using the setting Integrated Security=True;. This means that you're trying to connect using your Windows credentials. Are you sure you have the rights to connect with your Windows credentials? If not, remove this setting and add your username and password, like in this connection string:

此外,您正在使用设置集成安全性=True;。这意味着您正在尝试使用Windows凭据进行连接。您确定您拥有连接Windows凭据的权限吗?如果没有,请删除此设置并添加您的用户名和密码,如在此连接字符串中:

"Data Source=.\SQLEXPRESS;User Id=sa; Password=sa123; Connect Timeout=30;"

Read this interesting question about the use of Integrated Security setting.

阅读这个关于集成安全设置使用的有趣问题。

Also, your SQLEXPRESS instance seems to be a user instance. Is this instance enabled?

而且,您的SQLEXPRESS实例似乎是一个用户实例。这是启用实例吗?