110,534
社区成员
发帖
与我相关
我的任务
分享
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.SqlClient;
namespace datagridview
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string flagpack = "3";
SqlConnection conn = new SqlConnection(@"server = 192.168.1.159;database =testdb;UID=sa;PWD=062455xzy");
string sqlPack = "select top 1* from dbo.Table_1 Where flag=@GrdFlag order by id desc";
SqlCommand cmd = new SqlCommand(sqlPack, conn);
SqlParameter parameterGridFlag = new SqlParameter("@GrdFlag", SqlDbType.NVarChar);
parameterGridFlag.Value = flagpack;
cmd.Parameters.Add(parameterGridFlag);
try
{
conn.Open();
SqlDataAdapter da = new SqlDataAdapter(sqlPack, conn);
DataSet ds = new DataSet();
da.Fill(ds);
conn.Close();
////查询的数据显示到datagridview
string grdPackTime = ds.Tables[0].Rows[0]["functime"].ToString();
string grdBatch = ds.Tables[0].Rows[0]["batch"].ToString();
string grdSoftWare = ds.Tables[0].Rows[0]["software"].ToString();
string grdClientName = ds.Tables[0].Rows[0]["clientname"].ToString();
string grdSerialNo = ds.Tables[0].Rows[0]["serialno"].ToString();
string grdImei = ds.Tables[0].Rows[0]["imei"].ToString();
string grdModuleSn = ds.Tables[0].Rows[0]["modulesn"].ToString();
string grdflag = ds.Tables[0].Rows[0]["flag"].ToString();
string grdBox = ds.Tables[0].Rows[0]["box"].ToString();
string[] row = { grdPackTime, grdBatch, grdSoftWare, grdClientName, grdSerialNo, grdImei, grdModuleSn, grdflag, grdBox };
////给dataGridViewScan控件添加数据
dataGridViewPack.Rows.Add(row);
////datagridview显示排序-降序
this.dataGridViewPack.Sort(this.dataGridViewPack.Columns[0], ListSortDirection.Descending);
}
catch(SqlException ex)
{
MessageBox.Show(ex.Message + ex.StackTrace, "Exception Details");
conn.Close();
}
finally
{
conn.Close();
}
}
}
}
SqlDataAdapter da = new SqlDataAdapter(sqlPack, conn);
这一句改为
==>
SqlDataAdapter da = new SqlDataAdapter(cmd);