システムからデータを追加してデータベースに保存できる C# プログラムを作成しています。SQL Server 2008 R2 を使用しています。名前と住所の 2 つのテーブルを持つデータ グリッド ビューがあります。私がやりたいことは、必要な情報を入力した後、保存ボタンをクリックした後にデータベースに保存したいということです。
これは私のデータ グリッド ビューのスクリーンショットです。
テキストボックスからデータベースにデータを保存するコードがあります。しかし、私はそれにテキストボックスを使用しましたが、テキストボックスの代わりにデータグリッドビューを使用して同じプロセス(データベースにデータを保存する)を行う方法がわかりません。助けてください。ありがとう。
これは、テキスト ボックスからデータベースにデータを保存する際に使用したコードです。
string connectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
try
{
using (SqlConnection connect = new SqlConnection(connectionString))
{
SqlCommand command = new SqlCommand();
command.Connection = connect;
command.CommandText = "insert into customer(name, address) values(@name, @address)";
command.Parameters.Add(new SqlParameter("@name", SqlDbType.VarChar));
command.Parameters.Add(new SqlParameter("@address", SqlDbType.VarChar));
command.Parameters["@name"].Value = name.Text.ToLower();
command.Parameters["@address"].Value = address.Text.ToLower();
connect.Open();
}
}