2011-10-20 5 views
3

だから、C#でローカルデータベースに接続できません最初のコードブロックに**との間の部分にエラーを与えは、私が接続し、データベースにレコードを挿入するための次のコードを持って、

エラーがある:

System.Data.SqlClient.SqlException was unhandled 
    Message=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=.Net SqlClient Data Provider 
    ErrorCode=-2146232060 
    Class=20 
    LineNumber=0 
    Number=-1 
    Server="" 
    State=0 
    StackTrace: 
     at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection) 
     at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning() 
     at System.Data.SqlClient.TdsParser.Connect(ServerInfo serverInfo, SqlInternalConnectionTds connHandler, Boolean ignoreSniOpenTimeout, Int64 timerExpire, Boolean encrypt, Boolean trustServerCert, Boolean integratedSecurity) 
     at System.Data.SqlClient.SqlInternalConnectionTds.AttemptOneLogin(ServerInfo serverInfo, String newPassword, Boolean ignoreSniOpenTimeout, TimeoutTimer timeout, SqlConnection owningObject) 
     at System.Data.SqlClient.SqlInternalConnectionTds.LoginNoFailover(ServerInfo serverInfo, String newPassword, Boolean redirectedUserInstance, SqlConnection owningObject, SqlConnectionString connectionOptions, TimeoutTimer timeout) 
     at System.Data.SqlClient.SqlInternalConnectionTds.OpenLoginEnlist(SqlConnection owningObject, TimeoutTimer timeout, SqlConnectionString connectionOptions, String newPassword, Boolean redirectedUserInstance) 
     at System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, Object providerInfo, String newPassword, SqlConnection owningObject, Boolean redirectedUserInstance) 
     at System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions options, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection) 
     at System.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConnection owningConnection, DbConnectionPool pool, DbConnectionOptions options) 
     at System.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnection owningObject) 
     at System.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnection owningObject) 
     at System.Data.ProviderBase.DbConnectionPool.GetConnection(DbConnection owningObject) 
     at System.Data.ProviderBase.DbConnectionFactory.GetConnection(DbConnection owningConnection) 
     at System.Data.ProviderBase.DbConnectionClosed.OpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory) 
     at System.Data.SqlClient.SqlConnection.Open() 
     at Stockbeheer_Savenay.Forms.LeggerToevoegen.addItem(String leggernummer, String omschrijving) in C:\Users\Simon\Desktop\Stockbeheer Savenay\Stockbeheer Savenay\Forms\LeggerToevoegen.cs:line 59 
     at Stockbeheer_Savenay.Forms.LeggerToevoegen.btnToevoegen_Click(Object sender, EventArgs e) in C:\Users\Simon\Desktop\Stockbeheer Savenay\Stockbeheer Savenay\Forms\LeggerToevoegen.cs:line 31 
     at System.Windows.Forms.Control.OnClick(EventArgs e) 
     at System.Windows.Forms.Button.OnClick(EventArgs e) 
     at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent) 
     at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks) 
     at System.Windows.Forms.Control.WndProc(Message& m) 
     at System.Windows.Forms.ButtonBase.WndProc(Message& m) 
     at System.Windows.Forms.Button.WndProc(Message& m) 
     at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m) 
     at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m) 
     at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam) 
     at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg) 
     at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData) 
     at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context) 
     at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context) 
     at System.Windows.Forms.Application.Run(Form mainForm) 
     at Stockbeheer_Savenay.Program.Main() in C:\Users\Simon\Desktop\Stockbeheer Savenay\Stockbeheer Savenay\Classes\Program.cs:line 18 
     at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args) 
     at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args) 
     at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() 
     at System.Threading.ThreadHelper.ThreadStart_Context(Object state) 
     at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx) 
     at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) 
     at System.Threading.ThreadHelper.ThreadStart() 
    InnerException: 

私はC#のデータベースを初めて使っているので、何が間違っているのか分かりません。チュートリアルに従おうとしましたが、彼らはすべて異なったことを言っていますので、正確に何をすべきかを理解することは非常に難しいです。

ありがとうございます!

+0

SQL Server Management Studi o?もしそうなら、あなたは接続するためにどのような情報を使いましたか?一度接続すればあなたのDBは見えますか?そのDBの名前は何ですか?私はここであなたの接続文字列をデバッグする必要があると思います... –

+0

SQLEXPRESSサービスインスタンスは動作していますか? – kprobst

+0

@ MerlynMorgan-Graham:SQL Server Management Studioが何であるか分かりません。私はVisual Studioでデータベースを作成し、接続がそこで動作するので、私はテストしました。 DBの名前は単なる「データベース」です。 –

答えて

10

.sdfファイル(SQL ServerのCompact Editionの)を使用している場合、あなたはSqlCeConnectionを使用する必要はありませSqlConnection(フルSQL Serverに対して動作します):

string dbfile = "|DataDirectory|\\Databases\\Database.sdf"; 

string insertStmt = "insert into Leggers values('" + leggernummer + "','" + omschrijving + "')"; 

using(SqlCeConnection connection = new SqlCeConnection("Data Source=" + dbfile)) 
using(SqlCeCommand query = new SqlCeCommand(insertStmt, connection)) 
{ 
    connection.Open(); 
    query.BeginExecuteNonQuery(); 
    connection.Close(); 
} 

また:あなたは試してみてくださいのパラメータを使用し、SQL文を連結するだけではありません...(リサーチ "SQLインジェクション攻撃" ...)

+0

SqlCeConnectionとSqlCeCommandは認識されません。私は以下の指示文を使用しています:using System.Data; System.Data.SqlClientを使用している ; using System.Data.OleDb; また、プログラムはオフラインアプリケーションであり、1人のみが使用します。だから私はSQLインジェクションの危険性はない。 –

+0

さて、今接続しました。しかし、私のデータベースは、私が追加する新しいレコードで更新されていません。 –

+0

デバッグしているので、追加された内容は無視されますか? –

関連する問題