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

时间:2015-12-15 10:12:55

标签: 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();   
    }
}

1 个答案:

答案 0 :(得分:0)

为...提供ID /名称。 您可以使用该ID获取用户名以及获取present / absent.leave状态。

<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