Simple use of C# and SQLServer

The first step, the namespace:

using System.Data.SqlClient;

The second step is to declare variables

 public bool bConnect=false;//Database connection identification
SqlConnection con = new SqlConnection();

The third step is to connect to the database

public void ConnectSql()
{
            con.ConnectionString = "server=192.168.0.110,1433;Database=db;uid=sa;pwd=sa123;";
            try
            {
                con.Open();
            }
            catch (System.Exception ex)
            {
                string strErr = ex.ToString();
                MessageBox.Show(strErr);
                return;
            }
            MessageBox.Show("sql连接成功");
            bConnect = true;
 }

The fourth step, query data

          if (!bConnect)
            {                 MessageBox.Show("Database is not connected");                 return;             }             string strOrder="ORDER001";             if (textBox1.Text!="")             {                 strOrder = textBox1.Text;             }             string str3 = "" ;             string str26 = "";             string str27 = "";             string str28 = "";             //Create database command             SqlCommand cmd = con.CreateCommand();             //Create query statement             cmd.CommandText = "SELECT *FROM MOCTA where T002= '" + strOrder + "'";             //Read the data stream from the database and store it in the reader

















            SqlDataReader reader = cmd.ExecuteReader();
            //Read the next line of data from the reader, if there is no data, reader.Read() returns fasle
            while (reader.Read())
            {                if (reader["T026"]!= System.DBNull.Value)//Judging whether the read data is empty                {                    str26 = reader.GetString(reader.GetOrdinal("T026"));                }                else                {                    MessageBox.Show("T026 is empty");                }







               if (reader["T027"] != System.DBNull.Value)
               {   
                   str27 = reader.GetString(reader.GetOrdinal("T027"));
               }
               else
               {
                   MessageBox.Show("T027为空");
               }

               if (reader["T028"] != System.DBNull.Value)
               {
                   str28 = reader.GetString(reader.GetOrdinal("T028"));
               }
               else
               {
                   MessageBox.Show("T028为空");
               }

                if (reader["T003"]!=System.DBNull.Value)
                {
                    str3 = reader.GetString(reader.GetOrdinal("T003"));
                }
                else
                {
                    MessageBox.Show("为空");
                }

                Console.WriteLine(str3 + "  " + str26 + "  " + str27 + "  " + str28);
            }
            reader.Dispose();

Guess you like

Origin blog.csdn.net/Hat_man_/article/details/108323381