将数组保存到SQL数据库行 [英] save an array to SQL database row

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

问题描述

大家好,



我想将数组{12,ad,sd,wq,3e,d5,sd ,,, we}保存到SQL行。

SQL数据库中的列将来会发生变化。

在这种情况下,我有10列。

也许在未来几天我将有8列或11列。

前6列永远不会改变。



这是保存数据的最佳方法从数组到数据库中的新行?



预先感谢您的回复



Vidor

Hi all,

I want to save an array {12,ad,sd,wq,3e,d5,sd,,,we} to a SQL row.
The columns in the SQL database changes in the future.
In this case I have 10 columns.
Maybe in the future days I will have 8 columns or 11 columns.
The first 6 columns will never change.

Which is the best way to save the data from the array to the new row in the database?

Thanks in advance for replies

Vidor

推荐答案

您好,试试这个



如果您在Array和Data中有列名也在另一个数组中那么

Hi, Try This

If you have column names in Array and Data also in another array then
DECLARE @SqlStr VARCHAR(1000)
SELECT @SqlStr = 'INSERT INTO your_TableName('+@ColumName_Array+') VALUES ('+@ColumnValues_Array+')'
EXEX(@SqlStr)
-- So Later Colum List and Values Changes also No Problem. 
-- If only one row then it will works, If Multiple Rows then go for INSERT with SELECT Concept.



我认为它会对你有所帮助。 :-) :-) :-)



问候,

GVPrabu


I think it will helps u. :-) :-) :-)

Regards,
GVPrabu


我解决了问题,感谢 GVPrabu 指出了我正确的方向...



I resolved the problem, thanks to GVPrabu who pointed me to the right direction...

........
 string strColumns = sbColumn.ToString().TrimEnd(',');
 string[] input = sb.ToString().Split(',');
 string str = sb.ToString().TrimEnd(',');
 SqlConnection connSave = new SqlConnection(connString);
 string qryString = "INSERT INTO UserData1("+strColumns+") VALUES ("+str+");";
 CreateMySqlCommand(qryString, connSave);
 }

 public void CreateMySqlCommand(string myExecuteQuery, SqlConnection myConnection)
 {
   SqlCommand myCommand = new SqlCommand(myExecuteQuery, myConnection);
   myCommand.Connection.Open();
   myCommand.ExecuteNonQuery();
   myConnection.Close();
 }


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

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