我在C#中有一個表,數據來自Excel文件。我需要將這些數據插入到SQL Server 2000表中。我不會使用存儲過程。我如何編程?任何幫助,將不勝感激。使用內嵌查詢將數據插入到SQL Server數據庫表中,不允許使用存儲過程
0
A
回答
0
你有DataTable
?
你會需要像:
// set up connection to your database
using (SqlConnection con = new SqlConnection("your-connection-string-here"))
{
// define the INSERT statement - of course, I don't know what your table name
// is and which and how many fields you want to insert - adjust accordingly
string insertStmt =
"INSERT INTO dbo.YourTable(field1, field2, field3) " +
"VALUES(@field1, @field2, @field3)";
// create SqlCommand object
using (SqlCommand cmd = new SqlCommand(insertStmt, con))
{
// set up the parameters - again: I don't know your parameter names
// nor the parameters types - adjust to your needs
cmd.Parameters.Add("@field1", SqlDbType.Int);
cmd.Parameters.Add("@field2", SqlDbType.VarChar, 100);
cmd.Parameters.Add("@field3", SqlDbType.VarChar, 250);
// open connection
con.Open();
// iterate over all the Rows in your data table
foreach (DataRow row in YourDataTable.Rows)
{
// assign the values to the parameters, based on your DataRow
cmd.Parameters["@field1"].Value = Convert.ToInt32(row["columnname1"]);
cmd.Parameters["@field2"].Value = row["columnname2"].ToString();
cmd.Parameters["@field3"].Value = row["columnname3"].ToString();
// call INSERT statement
cmd.ExecuteNonQuery();
}
// close connection
con.Close();
}
}
當然,這有沒有錯誤檢查任何,你將需要添加一些的是自己(嘗試....漁獲物等) 。但基本上,如果我不能使用存儲過程,那就是我會這樣做的方式。
0
使用System.Data.SqlClient.SqlCommand
相關問題
- 1. 如何使用存儲過程將datetime插入到SQL Server數據庫表中?
- 2. 使用存儲過程將數據從SQL表插入到Excel
- 3. 允許未在SQL Server中使用查詢輸入數據
- 4. Android:使用webservices將數據插入到sql server數據庫
- 5. SQL Server 2005中的內置數據庫角色允許執行存儲過程?
- 6. 將數據插入SQL Server數據庫使用ADO.Net數據集
- 7. 在PowerShell中調用存儲過程以插入到SQL Server數據庫中
- 8. 使用查詢從程序或使用存儲過程插入/更新/刪除數據庫中的數據
- 9. 使用PHP將表單數據插入到sql server數據庫中
- 10. 從存儲過程中將數據表插入到數據庫表中
- 11. 在SQL Server中遇到數據查詢使用數據庫
- 12. 如何通過存儲過程將數據插入到sql數據表中
- 13. 使用存儲過程插入到數據庫
- 14. 使用存儲過程將多行插入到SQL Server 2005中
- 15. 使用PDO將數據插入到MySQL數據庫表中 - 不插入數據
- 16. 使用多個存儲過程將數據插入表
- 17. 使用PHP SQL Server數據庫查詢
- 18. 我想用C#將數據插入到SQL Server數據庫中。
- 19. sql server - 使用存儲過程使用服務器上所有內存的存儲過程的數據庫
- 20. 使用表格插入到sql server數據庫中
- 21. 檢查SQL Server數據庫中是否存在使用存儲過程的行
- 22. 使用Callable語句在數據庫中插入存儲過程
- 23. 使用存儲過程從硬盤備份SQL Server數據庫?
- 24. 如何使用SQL Server中的存儲過程在數據表中插入數據表?
- 25. 使用存儲過程將數據從MS Sql導入到MySQL
- 26. 允許用戶使用excel查詢OLTP SQL Server數據庫是否正常?
- 27. 將數據插入到使用存儲過程從其他表派生的表中插入數據
- 28. 使用存儲過程將json表數據保存到SQL Server數據庫(c#後端)
- 29. 存儲過程錯誤中不允許使用子查詢
- 30. 使用HTTP從Excel中將數據插入到SQL Server數據庫中Post
感謝marc爲您的及時迴應 – Developer 2010-04-23 19:48:50