如何在SQL Server Compact中使用INSERT语句 [英] How to use INSERT statement in SQL server compact
本文介绍了如何在SQL Server Compact中使用INSERT语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
请有人提供正确的语法和命令来帮助我,以将RichTextBox的每一行插入到名为valid_reg的数据库列中.还有如何将数据库列的内容复制到数组中.
Please can someone help me with the correct syntax and command to INSERT each line of a RichTextBox into a database column called valid_reg. Also how to copy the contents of the column of the database into an array.
推荐答案
获取 ^ ],然后对SQL进行插入.
Get each line in the RichTextBox.Lines[^] and do a INSERT to SQL.
sqlCon = new SqlConnection("ConnectionString");
sqlCon.Open();
foreach (string line in RichTextBox.Lines)
{
sql = "INSERT INTO tbl (valid_reg) VALUES(@inputText)";
sqlCmd = new SqlCommand(sql, sqlCon);
sqlCmd.Parameters.AddWithValue("@inputText", line);
sqlCmd.ExecuteNonQuery();
}
插入此类数据的最佳方法是使用一次保存所有记录并一次提交到数据库的数据表.我假设该数据库表名称为Registration
.
The best way to insert such kind of data is to use datatable that hold all the record at a time and commit into the database at once.I am assuming the database table name isRegistration
.
/// <summary>
/// Insert Registration
/// </summary>
public void InsertRegistration()
{
DataTable registration = new DataTable("Registration");
using (SqlConnection connection = new SqlConnection("connectionString"))
{
connection.Open();
SqlDataAdapter adapter = new SqlDataAdapter();
// Just for initializing the datatable
SqlCommand selectCommand = new SqlCommand("SELECT TOP 1 valid_reg FROM Registration", connection);
adapter.SelectCommand = selectCommand;
adapter.Fill(registration);
// Insert each line of RichTextBox to a DataTable
foreach (string line in richTextBox1.Lines)
{
DataRow newRegistration = registration.NewRow();
newRegistration["valid_reg"] = line;
registration.Rows.Add(newRegistration);
}
SqlCommand insertCommand = new SqlCommand("INSERT INTO Registration(valid_reg) VALUES(@valid_reg)", connection);
SqlParameter parameter = new SqlParameter("@valid_reg", SqlDbType.NVarChar);
insertCommand.Parameters.Add(parameter);
adapter.InsertCommand = insertCommand;
adapter.Update(registration); // Commit registration
}
}
希望对您有所帮助.
I hope this helps you well.
这是插入命令最常用的语法...
INSERT INTO tableName(field1,field2,field3 ... fieldN)VALUES(value1,value2,value3 .... valueN);
如果您可以写出您的字段名称和文本框名称,我可以为您提供插入命令的确切语法....
谢谢与问候,
Punit Parmar
This is most common syntax of insert command...
INSERT INTO tableName (field1,field2,field3...fieldN) VALUES (value1,value2,value3....valueN);
if you can write your field name and textbox Name, i can give you exact syntax of Insert command....
Thanks & Regards,
Punit Parmar
这篇关于如何在SQL Server Compact中使用INSERT语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文