我正在使用 ASP.NET,我想将 Excel 工作表导入到 SQL Server 中的表中。 问题是我的 Excel 的一列有百分比类型(例如:25%)。您是否有解决方案将该百分比值存储在我的 SQL Server 表中?
如果您愿意,我会发布我的代码:
protected void Button1_Click(object sender, EventArgs e)
{
if (FileUpload1.HasFile)
{
string path = string.Concat((Server.MapPath("~/temp/" + FileUpload1.FileName)));
FileUpload1.PostedFile.SaveAs(path);
OleDbConnection OleDbcon = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties=Excel 12.0;");
// Feuil1 est la première page du fichier Excel
OleDbCommand cmd = new OleDbCommand("SELECT * FROM [Feuil1$]", OleDbcon);
OleDbDataAdapter objAdapter1 = new OleDbDataAdapter(cmd);
OleDbcon.Open();
DbDataReader dr = cmd.ExecuteReader();
// configuration de la chaîne de connexion de SQL Server
string con_str = @"Data Source=.;Initial Catalog=projetDGCS;Integrated Security=True";
// On copie dans SQL Server
SqlBulkCopy bulkInsert = new SqlBulkCopy(con_str);
// On mets le nom de la table de destination
bulkInsert.DestinationTableName = "reponse";
bulkInsert.WriteToServer(dr);
OleDbcon.Close();
// On a stocké le fichier Excel dans le dossier "temp" et on le supprime après
Array.ForEach(Directory.GetFiles((Server.MapPath("~/temp/"))), File.Delete);
Label1.ForeColor = Color.Green;
Label1.Text = "Insertion réussie !";
}
最佳答案
Excel 没有字段的百分比类型。它具有数字字段的百分比格式 - 与您的应用程序可以应用的相同。 0.01 = 1%(至少我的学校是这么教的)。
这是非常客观的 - 您可以在任何数字字段中存储百分比值。如何格式化它以及是否使用例如tinyint(1 = 1%)完全取决于您作为程序员。
关于c# - sql server 中百分比列的数据类型是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24221253/