0

シンプルな登録フォームを作成しようとしています。テキスト ボックスから DataSet テーブルにデータを挿入したいのですが、ここに私の WebForm コードがあります。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.Sql;
using System.Configuration;
using System.Data.SqlClient;


namespace IknowyourbrainWebSite
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);


        protected void Page_Load(object sender, EventArgs e)
        {
            con.Open();
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            SqlCommand cmd = new SqlCommand("insert into tbl values('"+TxtUserName.Text+"','"+TxtPassword.Text+"','"+TxtRePassword.Text+"')", con);
            cmd.ExecuteNonQuery();
            con.Close();
            TxtUserName.Text = "";
            TxtPassword.Text = "";
            TxtRePassword.Text = "";


        }
    }
}

起動するとエラーが発生しました:

Server Error in '/' Application.
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: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified) 
 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.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: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified)

Source Error: 

Line 20:         protected void Page_Load(object sender, EventArgs e)
Line 21:         {
Line 22:             con.Open();
Line 23:         }
Line 24: 


 Source File:  c:\Users\Fluksikarton\Documents\Visual Studio 2012\Projects\IknowyourbrainWebSite\IknowyourbrainWebSite\WebForm1.aspx.cs    Line:  22 

Stack Trace: 

[SqlException (0x80131904): 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: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified)]
   System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction) +5295167
   System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose) +242
   System.Data.SqlClient.TdsParser.Connect(ServerInfo serverInfo, SqlInternalConnectionTds connHandler, Boolean ignoreSniOpenTimeout, Int64 timerExpire, Boolean encrypt, Boolean trustServerCert, Boolean integratedSecurity, Boolean withFailover) +5307115
   System.Data.SqlClient.SqlInternalConnectionTds.AttemptOneLogin(ServerInfo serverInfo, String newPassword, SecureString newSecurePassword, Boolean ignoreSniOpenTimeout, TimeoutTimer timeout, Boolean withFailover) +145
   System.Data.SqlClient.SqlInternalConnectionTds.LoginNoFailover(ServerInfo serverInfo, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance, SqlConnectionString connectionOptions, SqlCredential credential, TimeoutTimer timeout) +920
   System.Data.SqlClient.SqlInternalConnectionTds.OpenLoginEnlist(TimeoutTimer timeout, SqlConnectionString connectionOptions, SqlCredential credential, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance) +307
   System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, SqlCredential credential, Object providerInfo, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance, SqlConnectionString userConnectionOptions) +434
   System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions options, DbConnectionPoolKey poolKey, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection, DbConnectionOptions userOptions) +225
   System.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConnectionPool pool, DbConnectionOptions options, DbConnectionPoolKey poolKey, DbConnectionOptions userOptions) +37
   System.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnectionOptions userOptions) +558
   System.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnectionOptions userOptions) +67
   System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, UInt32 waitForMultipleObjectsTimeout, Boolean allowCreate, Boolean onlyOneCheckConnection, DbConnectionOptions userOptions, DbConnectionInternal& connection) +1052
   System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal& connection) +78
   System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal& connection) +167
   System.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions) +143
   System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource`1 retry) +83
   System.Data.SqlClient.SqlConnection.Open() +96
   IknowyourbrainWebSite.WebForm1.Page_Load(Object sender, EventArgs e) in c:\Users\Fluksikarton\Documents\Visual Studio 2012\Projects\IknowyourbrainWebSite\IknowyourbrainWebSite\WebForm1.aspx.cs:22
   System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +51
   System.Web.UI.Control.OnLoad(EventArgs e) +92
   System.Web.UI.Control.LoadRecursive() +54
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +772
enter code here

このエラーは、ルーターやモデムなどを再起動するたびに変更されるか、コード/オプションのどこかに問題があることがわかっている限り、パブリックIPが静的ではないという事実によって引き起こされる可能性がありますか?

web.config の接続文字列コードは次のとおりです。

<configuration>
    <connectionStrings>
        <add name="ConnectionString" connectionString="Data Source=|DataDirectory|\RegistrationDataBase.sdf"
            providerName="System.Data.SqlServerCe.4.0" />
    </connectionStrings>
    <system.web>
      <compilation debug="true" targetFramework="4.5" />
      <httpRuntime targetFramework="4.5" />
    </system.web>


</configuration>
4

2 に答える 2

0

このエラーは、接続文字列が正しく設定されていない場合に最も頻繁に発生します。サーバー名とインスタンスが正しいか、資格情報が正しいか、サーバーがリモート接続を受信するように設定されているか (コードと同じボックスにないと仮定)、それが正しいことを確認しましたか?

于 2013-03-13T19:59:13.790 に答える
0

エラーは通常、接続文字列が正しくありません。これをチェックしてください。

Button2_Click をこのように書き換えてみませんか。

protected void Button2_Click(object sender, EventArgs e)
{
            SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
        con.Open();
        SqlCommand cmd = new SqlCommand("insert into tbl values('"+TxtUserName.Text+"','" + TxtPassword.Text+"','"+TxtRePassword.Text+"')", con);
        cmd.ExecuteNonQuery();
        con.Close();
        TxtUserName.Text = "";
        TxtPassword.Text = "";
        TxtRePassword.Text = "";

}

いくつかの良い出発点:

接続文字列が有効かどうかを確認する方法は?

また

http://www.connectionstrings.com/

あなたが得ている実際のエラーを読んでください

[SqlException (0x80131904): SQL Server への接続を確立中に、ネットワーク関連またはインスタンス固有のエラーが発生しました。サーバーが見つからないか、アクセスできませんでした。インスタンス名が正しいこと、および SQL Server がリモート接続を許可するように構成されていることを確認してください。(プロバイダー: SQL ネットワーク インターフェイス、エラー: 26 - 指定されたサーバー/インスタンスの検索エラー)]

于 2013-03-13T20:01:31.840 に答える