数据库中的列表框数据 [英] Listbox data in database

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

问题描述

我有一个文本框和3个列表框,我想将此数据添加到sql server 2005中的数据库中.因此,我如何从列表框中将数据添加到数据库中.

推荐答案

亲爱的朋友,

以下链接将为您提供帮助:-

将列表框中的数据插入C#中的数据库中 [ ^ ]

http://stackoverflow.com/questions/8612821/insert-listbox-items-into-multiple-field-in-database-c-sharp [
Dear Friend,

Following links will help you out:-

Inserting data from listbox into database in C#[^]

http://stackoverflow.com/questions/8612821/insert-listbox-items-into-multiple-field-in-database-c-sharp[^]

Don''t forget to mark this as your answer if it helps you out.

Thanks


使用此


Use this


using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;
 
namespace test
{
public partial class MainForm : Form
{
  public MainForm()
  {
      InitializeComponent();
  }
 
  void Button1Click(object sender, EventArgs e)
  {
     SqlConnection conn = new SqlConnection(@"User ID=username; Password=pass; 
                    Initial Catalog=databasename; data source=localhost");
     SqlCommand cmd = conn.CreateCommand();
     foreach(ListItem i in listBox1.SelectedItem)
           {
               cmd.CommandText = "INSERT INTO TableName (columnName) VALUES('"+i.Text)+"')";
               cmd.ExecuteNonQuery();
           }
     conn.Close();
  }
}



请接受并投票,否则请返回您的答案
--Rahul D.



Plz accept and vote otherwise revert back with your answer
--Rahul D.


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

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