我在這裏和其他地方廣泛搜索了答案,但都沒有取得成功。當字符串正常出現時從DataTable DataRow獲取int
我使用OLEDB連接到電子表格和提取數據移植到一個數據庫,所有會相當順利,除了一個小細節。
有一個叫技能欄在哪裏領取入口通常是一個字符串,但是值有時會像334423一個int,當我嘗試將此值添加到新的數據庫,該條目是簡單的空白。
通常的做法,以提取:
foreach(DataRow dr in dt.Rows)
{
/* dealing with other columns */
sTemp.skill = dr[3].ToString();
/* more stuff */
}
但我得到完全沒有反應時,該行的列是一個int,只是一個空字符串。它甚至不會拋出任何例外,這至少會給我一些方向。
我也試着鑄造它作爲一個int:
sTemp.skill = (int)dr[3].ToString();
(沒有工作)
FYI: 「STEMP」 和與它相關的,技術是一串串的對象。這個問題的神祕安靜的性質也使我很難知道我應該問什麼。任何OLEDB大師,請幫忙!謝謝
編輯:經過一番調查後,似乎問題不是轉換/鑄造字符串到整數,而是實際上首先獲取數據。
在具有數字的字段的實例中,它將完全忽略它(因爲我想不出來的原因),所以沒有什麼可以解析的。神祕的行爲的確如此。
除非有人使用一個OleDbDataAdapter獲得從Excel整數已知問題知道,然後隨意回答......對不起,浪費了你的時間所有,多麼尷尬。
EDIT#2:這裏是更新的代碼視圖:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
namespace SQEPSkillsImporter
{
public partial class MainWindow : Form
{
string skillsFileName = "";
List<SkillTemplate> TemplateList = new List<SkillTemplate>();
public MainWindow()
{
InitializeComponent();
}
private void skillsFilePathBtn_Click(object sender, EventArgs e)
{
this.skillsFileDialog.Filter = "Excel Worksheets|*.xls;*xlsx";
DialogResult result = skillsFileDialog.ShowDialog();
if (result == DialogResult.OK)
{
this.skillsFileName = skillsFileDialog.FileName;
skillsFilePathTxBx.Text = skillsFileName;
}
}
private void readSkillsBtn_Click(object sender, EventArgs e)
{
DataTable skillsTable = new DataTable("SkillsResults");
string connectionString = "Provider=Microsoft.Jet.OleDb.4.0;" +
" Data Source=" + this.skillsFileName +
";Extended Properties=Excel 8.0;";
using(OleDbConnection Connection = new OleDbConnection(connectionString))
{
Connection.Open();
using(OleDbCommand Command = new OleDbCommand())
{
Command.Connection = Connection;
/* The columns I want are in A through D */
Command.CommandText = "SELECT * FROM [Skills$A:D]";
using (OleDbDataAdapter Adapter = new OleDbDataAdapter())
{
Adapter.SelectCommand = Command;
Adapter.Fill(skillsTable);
}
}
Connection.Close();
Connection.Dispose();
}
SkillTemplate sTemp = new SkillTemplate();
foreach (DataRow dr in skillsTable.Rows)
{
/* most sGroup, group and heading columns are blank, so remain the same unless changed, meaning the name wont be null */
if(dr[0].ToString() != "")
sTemp.SuperGroup = dr[0].ToString();
if (dr[1].ToString() != "")
sTemp.Group = dr[1].ToString();
if (dr[2].ToString() != "")
sTemp.Heading = dr[2].ToString();
if (dr[3].ToString() != "")
sTemp.Skill = dr[3].ToString();
/* VERY rough test textbox to verify that my output is correct before sending to DB */
this.skillsDumpTxBx.Text += sTemp.SuperGroup + " /// " + sTemp.Group + " /// " + sTemp.Heading + " /// " + sTemp.Skill + System.Environment.NewLine;
}
}
}
public class SkillTemplate
{
public string SuperGroup { get; set; }
public string Group { get; set; }
public string Heading { get; set; }
public string Skill { get; set; }
}
}
'(int)dr [3] .ToString();'不能工作,但這會:'int.Parse (dr [3] .ToString());' –