C# 将每个单选按钮选定的数据保存到数据库

标签 c# jquery mysql asp.net

我正在尝试将单选按钮选定的值保存到数据库;不知道该怎么做。我在 Google 上找到了此处显示的 C# 代码,我需要对其进行修改,以便如果设置了三组单选按钮,我需要使用 foreach 将所有单选按钮选定的值保存到数据库中。

<table id='attendence'>
<tr>
  <td>
    <input type="text" readonly="readonly" value="12KQA60079">
  </td>
  <td>
    <input type="text" readonly="readonly" value="ARUNA S">
  </td>
  <th>
    <input type="radio" class="present" name="Present1" value="Present">
  </th>
  <th>
    <input type="radio" class="absent" name="Present1" value="Absent">
  </th>
  <th>
    <input type="radio" class="leave" name="Present1" value="Leave">
  </th>
</tr>
<tr>
  <td>
    <input type="text" readonly="readonly" value="12KQA60080">
  </td>
  <td>
    <input type="text" readonly="readonly" value="ASHWINI S M">
  </td>
  <th>
    <input type="radio" class="present" name="Present2" value="Present">
  </th>
  <th>
    <input type="radio" class="absent" name="Present2" value="Absent">
  </th>
  <th>
    <input type="radio" class="leave" name="Present2" value="Leave">
  </th>
</tr>
<tr>
  <td>
    <input type="text" readonly="readonly" value="12KQA60220">
  </td>
  <td>
    <input type="text" readonly="readonly" value="Das">
  </td>
  <th>
    <input type="radio" class="present" name="Present3" value="Present">
  </th>
  <th>
    <input type="radio" class="absent" name="Present3" value="Absent">
  </th>
  <th>
    <input type="radio" class="leave" name="Present3" value="Leave">
  </th>
</tr>
</table>

C#

string sql = "INSERT INTO MyTable (Col1, Col2) VALUES (@attendence, @username)"; 

using (MySqlConnection con = new MySqlConnection(connectionString))
{  
    int retvalue;
    con.Open();   

    foreach (DataRow row in myTable.Rows)   
    {      
        MySqlCommand myCommand = new MySqlCommand();
        myCommand.Connection = con;      
        myCommand.CommandText = sql;      
        myCommand.Parameters.AddWithValue("@attendence", r["attendence"]);      
        myCommand.Parameters.AddWithValue("@username", r["username"]);              
        retvalue = myCommand.ExecuteNonQuery();   
    }
}

最佳答案

给出 的 id/名称。 您可以使用该 ID 来获取用户名以及获取出席/缺席/离开状态。

<input id="username1" type="text" readonly="readonly" value="ARUNA S">
<input id="radio1" type="radio" class="present" name="Present2" value="Present">
<input  id="radio2" type="radio" class="absent" name="Present2" value="Absent">
<input  id="radio3" type="radio" class="leave" name="Present2" value="Leave">

获取用户名

 username1.text

获取状态为 -

if(radio1.checked)
    attendance=radio1.value
else if(radio2.checked)
    attendance=radio2.value
else if(radio3.checked)
    attendance=radio3.value

关于C# 将每个单选按钮选定的数据保存到数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34286384/

相关文章:

jquery - HTML 列表 - 仅将 addClass() 应用于第一级 LI 元素(不是内部元素)

MySQL:显示每月销售额并包括没有销售额的月份

php - 同一行不同列中的复选框值php

c# - 标识方法的开始和结束

c# - 考试 70-484(开发 Windows 应用商店应用的基础知识)- XAML 中的应用/导航栏

javascript - 使用 jQuery 实现效果

php - 如何使用 jQuery 将数据发布到 Laravel Controller 方法中

php - 控制单击“赞”按钮后在 Facebook 个人资料中显示的屏幕截图和标题

c# - 流利的断言 : equivalence of sorted lists

即使主线程退出,C# 子线程仍在工作