用 C# 構建一個表單應用程式,用于將手機作為一個專案進行銷售。我有兩個單選按鈕,用戶可以根據他們想要現金或卡的付款方式進行檢查。
如何根據用戶的選擇將該資料插入到資料庫中?

uj5u.com熱心網友回復:
你可以試試這個
protected void Button1_Click(object sender, EventArgs e)
{
string cs = ConfigurationManager.ConnectionStrings["db"].ConnectionString;
SqlConnection cn = new SqlConnection(cs);
SqlCommand cmd = new SqlCommand();
cmd.Connection = cn;
cmd.CommandType = CommandType.Text;
cmd.CommandText = "insert into sample values(@payment)";
cmd.Parameters.Clear();
cmd.Parameters.AddWithValue("@payment", payment.SelectedValue);
if (cn.State == ConnectionState.Closed)
cn.Open();
cmd.ExecuteNonQuery();
cn.Close();
lblmsg.Text = "Data entered successfully!!!";
}
uj5u.com熱心網友回復:
如果有人需要,我找到了我自己問題的答案
try
{
var connection = getConnection();
connection.Open();
if (CashRadio.Checked == true)
{
var command = new SqlCommand
{
Connection = connection,
CommandText ="INSERT INTO type_payment(cash,card) values(1,0)"
};
command.Parameters.Clear();
int result = command.ExecuteNonQuery();
if (result > 0)
{
MessageBox.Show("Succsefully picked type");
}
else
{
MessageBox.Show("error");
}
}
else if (CardRadio.Checked == true)
{
var command = new SqlCommand
{
Connection = connection,
CommandText = "INSERT INTO type_payment(cash,card) values(0,1)"
};
command.Parameters.Clear();
int result = command.ExecuteNonQuery();
if (result > 0)
{
MessageBox.Show("Succesfully picked type");
}
else
{
MessageBox.Show("Error");
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
uj5u.com熱心網友回復:
盧卡,
下面試試看。添加 HTML 標記,如:
<asp:CheckBox ID="chkCash" runat="server" />
.cs 檔案添加以下代碼。
string Cash = chkCash.Checked ? "Y" : "N";
并發送或添加值之類的。
SqlCommand cmd = new SqlCommand("INSERT INTO Employees(Cash) VALUES(@Cash)"))
{
cmd.Parameters.AddWithValue("@Cash", Cash);
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/389098.html
