存储过程:
USE [MODProductReportTest]
GO
/****** Object: StoredProcedure [dbo].[YFp1] Script Date: 10/15/2019 12:36:27 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER procedure [dbo].[YFp1]
@fileName nvarchar(50)
AS
BEGIN
select @fileName 文件名,'厚度' + 线条 间距1,间距,厚度 into #T1 from YF_BMQ where 文件名[email protected]
DECLARE @sql_str NVARCHAR(4000)
DECLARE @sql_col NVARCHAR(4000)
select @sql_col=AUFNR from (select AUFNR=stuff((select ',['+间距+']'
from (select distinct 间距 from #T1) b for xml path('')),1,1,'')) a
SET @sql_str = 'SELECT * FROM #T1 p PIVOT (MAX(厚度)FOR 间距 IN('+ @sql_col +')) AS pvt' --行转列
exec sp_executesql @sql_str
END
winform界面调用存储过程:
private void ButtonClickSelect()
{
if (comboBox1.Text.Trim() == "")
{
MessageBox.Show("请输入文件名", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
dgv.DataSource = null;
string fileName = comboBox1.Text.Trim();
List<SqlParameter> paras = new List<SqlParameter>();
paras.Add(new SqlParameter("@fileName", fileName));
DataSet ds = new DataSet();
try
{
ds = DBHelper.RunProcedure("YFp1", paras);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
return;
}
dt = ds.Tables[0];
if (dt.Rows.Count > 0)
{
//按列名大小排序
List<int> strList = new List<int>();
int k = 0;
for (int i = 2; i < dt.Columns.Count; i++)
{
strList.Add(int.Parse(dt.Columns[i].ColumnName));
}
int[] strArray = strList.ToArray();
Array.Sort(strArray);
for (int i = 2; i < dt.Columns.Count; i++)
{
dt.Columns[strArray[k].ToString()].SetOrdinal(i);
k++;
}
dt.Columns[1].ColumnName = "间距";
DataTable dtH = BLL.UtilClass.GetHData(fileName);
if (dtH.Rows.Count > 0)
{
textBoxH1.Text = dtH.Rows[0][0].ToString();
textBoxH2.Text = dtH.Rows[0][1].ToString();
}
List<double> rowS = new List<double>();
List<double> rowMaxS = new List<double>();
List<double> rowAveS = new List<double>();
for (int i = 0; i < dt.Rows.Count; i++)
{
for (int j = 16; j < dt.Columns.Count-15; j++)//前15列和后15列不参与计算,否则会出现索引异常
{
if (dt.Rows[i][j].ToString() != "" && dt.Rows[i][j + 15].ToString() != "")
{
double s = Math.Abs((double.Parse(dt.Rows[i][j].ToString()) - double.Parse(dt.Rows[i][j - 14].ToString())) / 7.5
- (double.Parse(dt.Rows[i][j + 15].ToString()) - double.Parse(dt.Rows[i][j].ToString())) / 7.5);
rowS.Add(s);
}
}
if (rowS.Count > 0)
{
rowMaxS.Add(rowS.Max());
rowAveS.Add(rowS.Average());
rowS.Clear();
}
}
textBoxS1.Text = rowMaxS.Max().ToString();
textBoxS2.Text = rowAveS.Average().ToString();
userPageBar1.DataSource = dt;
userPageBar1.DataBind();
}
Type type = dgv.GetType();
PropertyInfo pi = type.GetProperty("DoubleBuffered", BindingFlags.Instance | BindingFlags.NonPublic);
pi.SetValue(dgv, true, null);
}
/// <summary>
/// 执行存储过程
/// </summary>
/// <param name="ProcName"></param>
/// <param name="paras"></param>
/// <returns></returns>
public static DataSet RunProcedure(string ProcName, List<SqlParameter> paras)
{
using (SqlConnection sqlCon = new SqlConnection(Util.Configure.ConnStr))
{
try
{
SqlCommand cmd = new SqlCommand(ProcName, sqlCon);
foreach (SqlParameter para in paras)
{
cmd.Parameters.Add(para);
}
cmd.CommandType = CommandType.StoredProcedure;
SqlDataAdapter dap = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
dap.Fill(ds);
return ds;
}
catch (Exception ex)
{
throw ex;
}
finally
{
sqlCon.Close();
}
}
}
public static string ConnStr = @"Data Source=10.0.XX.XX;Initial Catalog=XXX;uid=sa;pwd=123456";
效果图: