使用C#,.NET 4.0,SQL,Visual Studio 2010和SQL Server管理2008C#data.fill(轉換失敗)
嗨,大家好,好像是同樣的老容易解決問題101,但有些事情一直在竊聽我。
我的程序運行查詢並抓取數據並填充數據集。
我的一個字段被稱爲pg(產品組),並設置爲nvarchar(255)。 大多數pgs是像30,50,12等數字,但也有例外,如T1B和50a。
由於某種原因,我收到了轉換錯誤。
「將varchar值'T1B'轉換爲int時轉換失敗。
在我的代碼中,我使用的是data.fill();
是我還是看起來好像data.fill試圖將字段PG作爲一個整數來分配,因爲它的第一個記錄看起來像一個int? 說實話,我一直認爲填充將模仿任何SQL數據類型的等價物。
希望它的東西容易im即俯瞰。
如果你希望看到更多的代碼,請告訴我,我會編輯這篇文章。
非常感謝
SQL查詢
USE [ShaftData]
GO
/****** Object: StoredProcedure [dbo].[GetSalesParetotemp] Script Date: 03/14/2012 09:30:13 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[GetSalesParetotemp]
@acct varchar(255),
@From date,
@Too date
AS
SELECT i.Acct,
i.Name,
i.Document,
i.Part,
i.Qty,
i.Unit,
dbo.NEWPareto.Pareto,
i.pg,
dbo.MyPgTable.PgName,
i.[DateTime]
FROM
OPENQUERY(SACBAUTO, 'SELECT dbo.iHeads.acct,
dbo.iHeads.name,
dbo.iLines.Document,
dbo.iLines.Part,
dbo.iLines.Pg,
dbo.iLines.Qty,
dbo.iLines.unit,
dbo.iHeads.[DateTime]
FROM Autopart.dbo.iheads INNER JOIN Autopart.dbo.iLines ON
Autopart.dbo.Iheads.document = autopart.dbo.iLines.document
GROUP By dbo.iHeads.acct,
dbo.iHeads.name,
dbo.iLines.Document,
dbo.iLines.Part,
dbo.iLines.Pg,
dbo.iLines.Qty,
dbo.iLines.unit,
dbo.iHeads.[DateTime]
') i
left JOIN
dbo.NEWPareto
ON
i.Part collate SQL_Latin1_General_CP1_CI_AS = dbo.NEWPareto.Part
left JOIN
dbo.MyPgTable
ON
i.pg collate SQL_Latin1_General_CP1_CI_AS = dbo.MyPgTable.[pGroup]
WHERE
(i.[DateTime] BETWEEN @From AND @Too) AND
i.Acct = @acct
AND i.pg != 60
AND i.pg != 61
AND i.pg != 62
GROUP BY i.Acct,
i.Name,
i.Document,
i.Part,
i.Qty,
i.Unit,
dbo.NEWPareto.Pareto,
i.pg,
dbo.MyPgTable.PgName,
i.[DateTime]
而這裏的調用它的方法
try
{
string myConn = "Server=derp;" +
"Database=derp;" +
"uid=derp;" +
"pwd=derp;" +
"Connect Timeout=120;";
string acct;// test using 1560
SqlConnection conn = new SqlConnection(myConn);
SqlCommand Pareto = new SqlCommand();
BindingSource bindme = new BindingSource();
SqlDataAdapter adapt1 = new SqlDataAdapter(Pareto);
DataSet dataSet1 = new DataSet();
DataTable table1 = new DataTable();
acct = Acct;
string fromDate = this.dateTimePicker1.Value.ToString("MM/dd/yyyy");
string tooDate = this.dateTimePicker2.Value.ToString("MM/dd/yyyy");
Pareto.Connection = conn;
Pareto.CommandType = CommandType.StoredProcedure;
Pareto.CommandText = "dbo.GetSalesParetotemp";
Pareto.CommandTimeout = 120;
Pareto.Parameters.AddWithValue("@acct", acct);
Pareto.Parameters.AddWithValue("@from", fromDate);
Pareto.Parameters.AddWithValue("@too", tooDate);
SetCheckBoxValue(true);
SetPictureBoxVisibility(true);
//label1.Visible = true;
adapt1.Fill(dataSet1, "Pareto");
//label1.Visible = false;
SetCheckBoxValue(false);
SetPictureBoxVisibility(false);
SetDataGrid(true, dataSet1, "Pareto", DataGridViewAutoSizeColumnsMode.AllCells);
dataGridView1.AutoResizeColumns(
DataGridViewAutoSizeColumnsMode.AllCells);
}
catch (Exception execc)
{
MessageBox.Show("Whoops! Seems we couldnt connect to the server!"
+ " information:\n\n" + execc.Message + execc.StackTrace,
"Fatal Error", MessageBoxButtons.OK, MessageBoxIcon.Stop);
}
你可以粘貼一些相關的代碼嗎? :) – 2012-03-14 09:28:42
沒問題,這裏是查詢,這裏是使用它的方法 – lemunk 2012-03-14 09:33:25