データベースとテーブルを最大限に活用するための簡単なプロジェクトを作成しました。私はMenu->Project->Add New Item->Service-based Database
Users.mdf
データベースをプロジェクトに追加しました。このテーブルではテーブルUsers
を作成しました。私はデータベースへのアクセス方法を複数のスレッドで検索しましたが、これまでのところはうまくいきませんでした。私のコードは今このようになっています(私はYouTubeのチュートリアルのコードを手に入れました)。基本的にボタンをクリックすると、dataGridView
が埋め込まれます。 データベースからデータを取得する#
private void show_database_button_Click(object sender, EventArgs e)
{
string constring ="Server=(LocalDb)\v11.0;Database=Users.mdf;Trusted_Connection=true;MultipleActiveResultSets=true;";
SqlConnection conDataBase = new SqlConnection(constring);
SqlCommand cmdDataBase = new SqlCommand(" select * from Users.Users;", conDataBase);
SqlDataAdapter sda = new SqlDataAdapter();
sda.SelectCommand = cmdDataBase;
DataTable dbadataset = new DataTable();
sda.Fill(dbadataset);
BindingSource bSource = new BindingSource();
bSource.DataSource = dbadataset;
dataGridView1.DataSource = bSource;
sda.Update(dbadataset);
}
そして、私はボタンをクリックしたときに私が手:
An unhandled exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll
Additional information: 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)
は、私がここで間違って何をしているのですか? (私がプロキシ接続をしていることを言及するには、それが気にするものであれば分かりません)。
接続文字列に問題があります。接続文字列を確認してください。 –
提案をお寄せいただきありがとうございます。@ un-lucky最初の問題だったので、 'Database - >(右クリック)Properties-> Connection String(copy)'を使って接続文字列を取得しました。今度は別のエラーが発生しました。 'ファイル名の自動データベースを接続しようとしました... ' –
' User Interface = True'を追加できません。 –