所以當我的表單加載時,它會連接到數據庫,當我點擊按鈕時,它會插入一個新行到我的數據庫,但我後我點擊它,我沒有看到我的表中有任何更改。無法在C#sql數據庫中插入數據
namespace database
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
SqlConnection con;
private void Form1_Load(object sender, EventArgs e)
{
con = new SqlConnection();
con.ConnectionString =
"Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\myworkers.mdf;Integrated Security=True;User Instance=True";
con.Open();
MessageBox.Show("OPEN!");
}
private void label1_Click(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
int x;
SqlCommand thiscommand = con.CreateCommand();
thiscommand.CommandText =
"INSERT INTO player_info (player_id, player_name) values (10, 'Alex') ";
x = thiscommand.ExecuteNonQuery(); /* I used variable x to verify
that how many rows are
affect, and the return is 1.
The problem is that I don't
see any changes in my table*/
MessageBox.Show(x.ToString());
con.Close();
}
}
}
你用什麼來檢查記錄是否被插入? SQL Server管理工作室? – 2012-03-25 20:12:28
您是否將用戶名和密碼留下來發布問題,或者他們不在代碼中? – mowwwalker 2012-03-25 20:14:29
你爲什麼要在Form_Load中打開連接?打開,使用並關閉(可能需要處理)需要它們的連接。 – 2012-03-25 20:14:41