将数据存储到数据库中的问题 [英] problem with storing data into database

查看:70
本文介绍了将数据存储到数据库中的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个下拉列表和一个文本框,无论我们选择还是输入,都应存储到数据库中.意味着如果我选择下拉列表,则所选值将存储到数据库中,或者如果我在文本框中输入值,则应将其存储到数据库中.当时我们只能使用一种方法dropdowlist或textbox来在相同的数据库字段中存储相同的值..

i have one dropdownlist and one textbox whatever we select or enter it should be store into database..means if i select dropdownlist then selected value will store into database or if i enter value into textbox it sholud be store into database..at time we can use only one means dropdowlist or textbox for storing same value in same database field..

推荐答案

像这样尝试
try like this
If TextBox1.Text Is Nothing Then
            cmd = New SqlCommand("insert into sample   values('" + DropDownList1.SelectedItem.Text + "')")
        Else
            cmd = New SqlCommand("insert into sample   values('" + TextBox1.Text + "')")
        End If
cmd.ExecuteNonQuery ()


至于逻辑部分:

as for the logic part:

if (TextBox1.Text.Trim() != string.Empty)
        {
            //The text box is not empty so let us use this value to push in DB
        }
        else
        {
            string valueToPush = DropDownList1.SelectedValue;
            //Now push this value in the DB
        }



对于数据库交互代码,您可以参考以下内容:

了解ADO.NET的初学者教程 [ ^ ]



and for the database interaction code you can refer this:

A Beginner''s Tutorial for Understanding ADO.NET[^]


在保存数据时(如果您的页面未按下拉列表选择回发),而不是检查文本框中的数据是否存在,而不是保存文本框数据或不保存下拉列表选择的值数据.
when you saving data if your page is not postback on dropdownlist selection than check that if in textbox data exist than save textbox data or not than save dropdownlist selected value data.


这篇关于将数据存储到数据库中的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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