我學習這個我自己,我已經到了一個地步,我想插入新的類別和新的國家,但我不知道該怎麼做究竟。
如用於增加一個新的類別我做到以下幾點:
public int Insert()
{
string sqlString = "INSERT INTO Categories (name, image) VALUES (@Name, @Image);";
SqlConnection sqlConnection = new
SqlConnection(ConfigurationManager.ConnectionStrings["OahuDB"].ConnectionString);
SqlCommand sqlCommand = new SqlCommand(sqlString, sqlConnection);
sqlCommand.Parameters.AddWithValue("@Name", this.Name);
sqlCommand.Parameters.AddWithValue("@Image", this.Image);
sqlConnection.Open();
int x = sqlCommand.ExecuteNonQuery();
sqlConnection.Close();
sqlConnection.Dispose();
return x;
}
但是我應該如何同時插入表之間的關係,然後檢索基於聯結表中的數據?
如果你可以給這個例子和好的教程,或者你可以詳細說明一下。 非常感謝。
爲什麼不使用存儲過程並通過C#調用它。 [這裏](http://stackoverflow.com/questions/5762942/stored-procedure-to-insert-two-tables-with-relationship)就是一個例子,要了解更多關於聯結表的信息,請參考[this](http ://megocode3.wordpress.com/2008/01/04/understanding-a-sql-junction-table/) –