C#データベースの追加、削除、変更

666 ワード

string str = "server=NO1;database=SuperMarket;integrated security=true";
            SqlConnection con = new SqlConnection(str);
            con.Open();

データベース接続を確立して開く
更新:
 SqlCommand cmd = new SqlCommand();//新sqlのコマンド
cmd.CommandText = "update food set foodnum='"+sheng+"' where foodid='"+ bh + "'";//彼の内容を設定して、行う操作
cmd.Connection = con;//接続を上のcon cmdに設定します.ExecuteNonQuery();//実行
/*上のコードは、SqlCommand cmd=new SqlCommand(「update food set foodnum='」+sheng+「'where foodid='」+bh+「'」,con)と書くこともできます.
*/
同じ理屈を追加して削除するには、CommandTextの内容を変更し、操作するだけです.