filtering in c# using sql server as database
filtering in c# using sql server as database
for example choosing fi开发者_如何学编程rst what to filter in the combobox by choosing student number and in the textfield entering 1001 ...den only 1001 will appear in the datagrid...
we are using ssql server
With a vague question like this I can only point you into the direction of LINQ to SQL http://msdn.microsoft.com/en-us/library/bb425822.aspx
This is an old way of doing this, It is for an idea, what steps are involved. It will work fine but I will advise to look into Entity Framework / Datasets. Table Adapters
// Read from file
FileStream fs;
fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);
// Conver to binary
byte[] binaryData = new byte[fs.Length];
fs.Read(binaryData, 0, System.Convert.ToInt32(fs.Length));
fs.Close();
// insert into DB
string connstr = @"Data Source=.;Initial Catalog=TestImage;
Persist Security Info=True;User ID=sa";
SqlConnection conn = new SqlConnection(connstr);
conn.Open();
string query = "insert into test_table(binaryCoulumn) values(" + binaryData + "+)";
SqlParameter parameter = new SqlParameter();
parameter.SqlDbType = SqlDbType.Binary;
parameter.ParameterName = "binaryCoulumn";
parameter.Value = binaryData;
SqlCommand cmd = new SqlCommand(query, conn);
cmd.Parameters.Add(parameter);
cmd.ExecuteNonQuery();
cmd.Dispose();
conn.Close();
conn.Dispose();
Hope this helps
here is her code why malou17 has an error malou17 code
when malou17 runs the program...and filter 1001 in student number...it will appear in the datagrid but the old data are still there..for example 1002 is still in the datagrid
}
private void button2_Click(object sender, EventArgs e)
{
if (cbofilter.SelectedIndex == 0)
{
string sql;
SqlConnection conn = new SqlConnection();
conn.ConnectionString = "Server= " + Environment.MachineName.ToString() + @"\; Initial Catalog=TEST;Integrated Security = true";
SqlDataAdapter da = new SqlDataAdapter();
DataSet ds1 = new DataSet();
ds1 = DBConn.getStudentDetails("sp_RetrieveSTUDNO");
sql = "Select * from Test where STUDNO like '" + txtvalue.Text + "'";
SqlCommand cmd = new SqlCommand(sql, conn);
cmd.CommandType = CommandType.Text;
da.SelectCommand = cmd;
da.Fill(ds1);
dbgStudentDetails.DataSource = ds1;
dbgStudentDetails.DataMember = ds1.Tables[0].TableName;
dbgStudentDetails.Refresh();
}
else if (cbofilter.SelectedIndex == 1)
{
//string sql;
//SqlConnection conn = new SqlConnection();
//conn.ConnectionString = "Server= " + Environment.MachineName.ToString() + @"\; Initial Catalog=TEST;Integrated Security = true";
//SqlDataAdapter da = new SqlDataAdapter();
//DataSet ds1 = new DataSet();
//ds1 = DBConn.getStudentDetails("sp_RetrieveSTUDNO");
//sql = "Select * from Test where Name like '" + txtvalue.Text + "'";
//SqlCommand cmd = new SqlCommand(sql,conn);
//cmd.CommandType = CommandType.Text;
//da.SelectCommand = cmd;
//da.Fill(ds1);
// dbgStudentDetails.DataSource = ds1;
//dbgStudentDetails.DataMember = ds1.Tables[0].TableName;
//ds.Tables[0].DefaultView.RowFilter = "Studno = + txtvalue.text + ";
dbgStudentDetails.DataSource = ds.Tables[0];
dbgStudentDetails.Refresh();
}
精彩评论