我寫了一段代碼,它會自動從我的SQL Server 2012 Express表中登錄並讀取信息。但它不會工作,這裏是代碼:SQL服務器express,無法讀取或寫入C#
private void Form1_Load(object sender, EventArgs e)
{
SqlConnection myConnection = new SqlConnection("user id=myComputer;" + "server=MYCOMPUTER-PC\\SQLEXPRESS;" +
"Trusted_Connection=yes;" + "database=loginTest; " + "connection timeout=5");
try
{
myConnection.Open();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
SqlCommand myCommand = new SqlCommand("INSERT INTO dbo.Logins Values ('John','Password','Admin')", myConnection);
try
{
SqlDataReader myReader = null;
SqlCommand myCommand1 = new SqlCommand("select * from Logins",
myConnection);
myReader = myCommand1.ExecuteReader();
while (myReader.Read())
{
MessageBox.Show(myReader["Column1"].ToString());
MessageBox.Show(myReader["Column2"].ToString());
}
}
catch (Exception ex1)
{
MessageBox.Show(ex1.ToString());
}
}
我已經調試的程序,這一切似乎都要經過精細,它跳過:
{
MessageBox.Show(myReader["Column1"].ToString());
MessageBox.Show(myReader["Column2"].ToString());
}
出於某種原因,並且它不寫我告訴它的價值。
有誰能告訴我爲什麼? IM在SQL初學者,這樣下去容易請:)
PS它不解僱任何錯誤代碼或異常
你永遠[執行](HTTP:/ /msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.executenonquery.aspx)'INSERT'命令 – GolfWolf
offtopic提示:儘量不要用這麼多的try-catch塊污染你的代碼。 – GolfWolf