あなたは、両方のコマンドにで表彰を実行できます。
try
{
connection.Open();
MessageBox.Show("Payment approved.");
richTextBox1.Text = richTextBox1.Text + "The hotel received " + txtbx9 + " from this guest";
string rtb = richTextBox1.Text.ToString();
command.Connection = connection;
command.CommandText = "INSERT INTO billing(g_name,g_surname,g_company,g_totalrate, g_paid, g_typepaid, info, u_add, u_tadd, g_ad, g_dd, g_amountofdays) VALUES('" + name + "','" + surname + "','" + company + "','" + txtbx8 + "', '" + txtbx9 + "', '" + cmbbx2 + "', '" + rtb + "', '" + label12.Text.ToString() + "', '" + this.dateTimePicker1.Value +"','"+textBox4.Text.ToString()+"','"+textBox5.Text.ToString()+"','"+textBox6.Text.ToString()+"')";
command.CommandText += "\nUPDATE guestreg SET g_paidstatus='Paid '"+txtbx9+"'' where g_name ='"+name+"' and g_status = '"+sts"'";
command.ExecuteNonQuery();
}
それとも、他の後にそれらを1つずつ実行します。
try
{
connection.Open();
MessageBox.Show("Payment approved.");
richTextBox1.Text = richTextBox1.Text + "The hotel received " + txtbx9 + " from this guest";
string rtb = richTextBox1.Text.ToString();
command.Connection = connection;
command.CommandText = "INSERT INTO billing(g_name,g_surname,g_company,g_totalrate, g_paid, g_typepaid, info, u_add, u_tadd, g_ad, g_dd, g_amountofdays) VALUES('" + name + "','" + surname + "','" + company + "','" + txtbx8 + "', '" + txtbx9 + "', '" + cmbbx2 + "', '" + rtb + "', '" + label12.Text.ToString() + "', '" + this.dateTimePicker1.Value +"','"+textBox4.Text.ToString()+"','"+textBox5.Text.ToString()+"','"+textBox6.Text.ToString()+"')";
command.ExecuteNonQuery();
command.CommandText = "UPDATE guestreg SET g_paidstatus='Paid '"+txtbx9+"'' where g_name ='"+name+"' and g_status = '"+sts"'";
command.ExecuteNonQuery();
}
編集:
スティーブが言及したように(そして彼は絶対に正しい)、パラメータを渡す必要がありますSqlParameters
となります。
try
{
connection.Open();
MessageBox.Show("Payment approved.");
richTextBox1.Text = richTextBox1.Text + "The hotel received " + txtbx9 + " from this guest";
string rtb = richTextBox1.Text.ToString();
command.Connection = connection;
command.CommandText = "INSERT INTO billing(g_name,g_surname,g_company,g_totalrate, g_paid, g_typepaid, info, u_add, u_tadd, g_ad, g_dd, g_amountofdays) VALUES(@name,@surname,@company,@txtbx8,@txtbx9,@cmbbx2,@rtb,@label12Text,@dateTimePicker1Value,@textBox4Text,@textBox5Text,@textBox6Text')";
command.Parameters.Add(new SqlParameter("@name",name));
command.Parameters.Add(new SqlParameter("@surname",surname));
command.Parameters.Add(new SqlParameter("@company",company));
command.Parameters.Add(new SqlParameter("@txtbx8",txtbx8));
command.Parameters.Add(new SqlParameter("@txtbx9",txtbx9));
command.Parameters.Add(new SqlParameter("@cmbbx2",cmbbx2));
command.Parameters.Add(new SqlParameter("@rtb",rtb));
command.Parameters.Add(new SqlParameter("@label12Text",label12.Text.ToString()));
command.Parameters.Add(new SqlParameter("@dateTimePicker1Value",this.dateTimePicker1.Value.ToString()));
command.Parameters.Add(new SqlParameter("@textBox4Text",textBox4.Text.ToString()));
command.Parameters.Add(new SqlParameter("@textBox5Text",textBox5.Text.ToString()));
command.Parameters.Add(new SqlParameter("@textBox6Text",textBox6.Text.ToString()));
command.ExecuteNonQuery();
command.CommandText = "UPDATE guestreg SET [email protected] where g_name [email protected] and g_status = @status";
command.Parameters.Add(new SqlParameter("@paidStatus","Paid " + txtbx9));
command.Parameters.Add(new SqlParameter("@name",name));
command.Parameters.Add(new SqlParameter("@status",sts));
command.ExecuteNonQuery();
}
あなたは二 'command.Connection =接続を削除することができ、 connection.Open(); 'は、コマンドを変更してもう一度実行し、その後は接続を閉じることを忘れないでください。 –
これはMySql、Sql Server、またはMS-Access用ですか? MySql/Sql Serverでは、2つのSQL文を使用して1つのコマンドを作成できます。これはアクセスでは不可能です。あなたの問題は、接続がすでに開いているときに開いている2番目の接続です。 – Steve
@stuartd 2番目の 'ExecuteNonQuery'の前に' try {'の直後にもう1つあります。 –