天天看点

c# select数据库数据

 public class PMInfobyPMHZ

    {

        static string DBstr = @"Server=tcp:*****;Initial Catalog=****;Persist Security Info=False;User ID=****;Password=****;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;";

        public string DM, PMHZ, WPDM, ZCJH, LDJH;

        public PMInfobyPMHZ(string PMHZ1)

        {

            if (PMHZ1.Length == 0)

            { DM = ""; PMHZ = ""; WPDM = ""; ZCJH = ""; LDJH = ""; }

            else

            {

                PMHZ = PMHZ1;

                SqlConnection conn1 = new SqlConnection(DBstr);

                string cmdText1 = @"select * from PM where PMHZ = '" + PMHZ1.Trim() + "'";

                SqlCommand command1 = new SqlCommand(cmdText1, conn1);

                try

                {

                    conn1.Open();

                    SqlDataReader reader1 = command1.ExecuteReader();

                    while (reader1.Read())

                    {

                        if (reader1["DM"] != DBNull.Value)

                        { DM = (reader1.HasRows) ? reader1.GetString(0) : string.Empty; }

                        if (reader1["ZCJH"] != DBNull.Value)

                        { ZCJH = (reader1.HasRows) ? reader1.GetString(3) : string.Empty; }

                        if (reader1["LDJH"] != DBNull.Value)

                        { LDJH = (reader1.HasRows) ? reader1.GetString(4) : string.Empty; }

                        if (reader1["WPDM"] != DBNull.Value)

                        { WPDM = (reader1.HasRows) ? reader1.GetString(10) : string.Empty; }

                    }

                    reader1.Close();

                }

                catch

                {

                }

                finally

                { conn1.Close(); }

            }

        }

    }