我的winforms有问题,当我通过存储过程将数据表传递给sql-server时,我收到了与标题问题相同的错误。 有人可以帮我解决吗?
类dbHelper中的方法ExecDB连接到sql并传递数据:
public static DataSet ExecDB(string procName, params object[] obj)
{
SqlConnection sqlCon = new SqlConnection(conString);
if (sqlCon.State == ConnectionState.Closed) sqlCon.Open();
SqlDataAdapter da = new SqlDataAdapter(procName, sqlCon);
da.SelectCommand.CommandType = CommandType.StoredProcedure;
SqlCommandBuilder.DeriveParameters(da.SelectCommand);
if (da.SelectCommand.Parameters.Count - 1 != obj.Length) throw new Exception("Error");
int index = 0;
foreach (SqlParameter pr in da.SelectCommand.Parameters)
{
if (pr.Direction == ParameterDirection.Input || pr.Direction == ParameterDirection.InputOutput)
{
pr.Value = obj[index++];
//pr.SqlDbType = SqlDbType.Structured;
}
}
DataSet ds = new DataSet();
da.Fill(ds);
if (sqlCon.State == ConnectionState.Open) sqlCon.Close();
return ds;
}
我的DataTable将通过:
DataTable dt = new DataTable();
dt.Clear();
dt.Columns.Add("NgayGiam", typeof(DateTime));
dt.Columns.Add("Tile", typeof(int));
dt.Columns.Add("Thang", typeof(string));
dt.Columns.Add("MaMon", typeof(int));
DataRow dr = dt.NewRow();
dr["NgayGiam"] =Convert.ToDateTime(i.SubItems[0].Text);
dr["Tile"] =Convert.ToInt32(tbxTiLeGiam.Text);
dr["Thang"] = twoMonth.Substring(twoMonth.Length - 2) + DateTime.Now.Year;
dr["MaMon"] = Convert.ToInt32(i.Tag.ToString());
dt.Rows.Add(dr);
这里的SQL结构:
CREATE TYPE BANGGIAMGIA AS TABLE(
NgayGiam DATE NOT NULL,
Tile INT NOT NULL,
Thang VARCHAR(6) NOT NULL,
MaMon INT NOT NULL
)
在SQL中使用proc:
create proc sp_UpdateGiamGia
@temp as BANGGIAMGIA ReadOnly
as
begin
Insert into GIAMGIA Select b.NgayGiam, b.Tile, b.Thang, b.MaMon from @temp as b
Select * from GIAMGIA where MONTH(Ngaygiam) = Month(Getdate()) and year(NgayGiam) = Year(getdate())
end
我不明白为什么会这样。请向我解释。
感谢。