我们可以从数据库中检索单选按钮的值 [英] Can we retreive the value of radio-button from database

查看:58
本文介绍了我们可以从数据库中检索单选按钮的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从数据库中获取单选按钮的值,但无法得到它,虽然我很容易将单选按钮的值插入数据库。例如:-inserting through

[ if(radioButton1.checked == true){} else {}],告诉我获取radiobutton的值,

即从后端到前端



我尝试了什么:



我在转换单选按钮

I am trying to get the value of radio button from database but can't get it,although I am easily inserting the value of radio button to database.for example:-inserting through
[if(radioButton1.checked==true){}else{}],tell me to get the value of radiobutton,
ie,from back end to front end

What I have tried:

I am facing no problem in converting the value of radio-button

推荐答案

查看此代码 -



表格设计 -

See this code -

Form design -
Take 1 lable and name is as "Do you like this app?"
Take two radio button - "Yes" and "No"
Create one button - "Ok"





创建此表 -



Create this table -

Create table tblFeedback (val NVARCHAR(10))





以下是代码 -



Below is the code -

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string connetionString = null;
            SqlConnection cnn;
            connetionString = @"Data Source=RDBurmon\SQLEXPRESS;Initial Catalog=Test;User ID=Test;Password=Test@123";

            cnn = new SqlConnection(connetionString);
            cnn.Open();


            String Deletequery = "Delete from tblFeedback";
            SqlCommand Deletecommand = new SqlCommand(Deletequery, cnn);
            Deletecommand.ExecuteNonQuery();

            String Insertquery;
            if (radioButton1.Checked == true)
                Insertquery = "INSERT INTO tblFeedback VALUES ('Yes')";
            else
                Insertquery = "INSERT INTO tblFeedback VALUES ('No')";

            SqlCommand Insertcommand = new SqlCommand(Insertquery, cnn);
            Insertcommand.ExecuteNonQuery();
            cnn.Close();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

            using (SqlConnection cnn = new SqlConnection(@"Data Source=RDBurmon\SQLEXPRESS;Initial Catalog=Test;User ID=Test;Password=Test@123"))
            {
                cnn.Open();

                using (SqlCommand cmd = new SqlCommand("select * from tblFeedback", cnn))
                {
                    using (SqlDataReader reader = cmd.ExecuteReader())
                    {
                        if (reader != null)
                        {
                            while (reader.Read())
                            {
                                if (reader[0].ToString() == "Yes")
                                    radioButton1.Checked = true;
                                else
                                    radioButton2.Checked = true;
                            }
                        }
                    } // reader closed and disposed up here

                } // command disposed here

            } //connection closed and disposed here

        }
    }
}


这篇关于我们可以从数据库中检索单选按钮的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆