我是新手,但我在SQL Server中有一個簡單的表,其中有一列name
和兩行。我試圖在按下按鈕時顯示名稱,我需要知道如何將SQL Server數據庫連接到C#代碼,以及如何從特定表讀取。
namespace WinFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string connetionString;
SqlConnection cnn;
connetionString = @"Data Source=DESKTOP-REB699D\SQLEXPRESS;Initial Catalog=DBdemo";
cnn = new SqlConnection(connetionString);
cnn.Open();
MessageBox.Show("Connection Open !");
cnn.Close();
}
}
}
但這沒有奏效,它在cnn.open();
行代碼處崩潰。
如果您沒有使用任何憑據,則為
windows authentication
。將
Integrated Security=True
添加到連接字符串有幫助嗎?您可以使用
Windows authentication
選項在SSMS上無憑據登錄數據庫嗎?如果是這樣,則不需要用戶憑據即可連接到DB。如果您需要用戶名和密碼,SQL Server and Windows Authentication
選項才能在沒有憑據的情況下訪問數據庫