【发布时间】:2022-01-22 01:02:54
【问题描述】:
【问题讨论】:
标签: c# sql forms radio-button
【问题讨论】:
标签: c# sql forms radio-button
你可以试试这个,
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!!!";
}
【讨论】:
如果有人需要,我找到了我自己问题的答案
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);
}
【讨论】:
卢卡,
在下面试试。 添加 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);
}
【讨论】: