从C#的checkedlistbox中选择时,如何在sql的insert commamd中传递表字段名 [英] how to pass tables field name in insert commamd of sql when selected from checkedlistbox in C#

查看:79
本文介绍了从C#的checkedlistbox中选择时,如何在sql的insert commamd中传递表字段名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在使用C#在窗口表单应用程序中从checkedlistbox中选择的sql的插入commamd中传递表字段名称


说明---



我想使用插入查询在特定的表中插入一些值。但是从CheckedListBox中选择了该表的字段名...





插入Tablename(??????????)值()



而不是?我要从Checkedlistbox中选择字段名称....

Plz help ....

how to pass tables field names in insert commamd of sql which was selected from checkedlistbox in Window Form application using C#

Explanation---

I want to use insert query to insert some values in particular table.But the field names of that table was selected from CheckedListBox...


insert into Tablename(??????????)values()

Instead of ? ihave to select the field names from Checkedlistbox....
Plz help....

推荐答案





你可以进行动态SQL查询。

准备如下字符串。

Hi,

you can go for dynamic SQL queries.
prepare the string like below.
string strQuery = "INSERT INTO TableName(";
int count =0;
foreach(object itemChecked in checkedListBox1.CheckedItems)
{
    strQuery += itemChecked.ToString();  //column names from the checked list
    count++;
    if(checkedListBox1.CheckedItems.Count > count)
        strQuery += ","; 
}
strQuery += ")VALUES(";
for(int i =0;i<checkedlistbox1.checkeditems.count; i++)
{
    if(i != 0)
        strQuery +=",";
    strQuery += "Value " + i.ToString(); // your values to the table
}





将该字符串发送到存储过程,并在过程中执行如下查询。



send that string to stored procedure and in the procedure execute the query like below.

EXEC(@Query)
--Where @Query would be input parameter





请参阅下面的链接,了解有关动态sql的更多信息。

在SQL Server中执行动态SQL命令 [ ^ ]

动态SQL [ ^ ]



希望它有所帮助。



refer links below for more on dynamic sql.
Execute Dynamic SQL commands in SQL Server[^]
Dynamic SQL[^]

hope it helps.


这篇关于从C#的checkedlistbox中选择时,如何在sql的insert commamd中传递表字段名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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