SqlConnection con = new SqlConnection(); string s = System.Environment.CurrentDirectory; //exe 所在路径 int i = 0; while (i < 0) { i++; int add = s.LastIndexOf(@"\"); s = s.Substring(0, add); } s += "\\ERP_IIP.mdf"; try { con.ConnectionString = "Data Source=.\\;AttachDbFilename=" + s + ";Integrated Security=True;User Instance=false"; con.Open(); DataSet ds = new DataSet(); string SQLString = "SELECT TOP (2) ID, USERID, PWD, WORKERCODE, USERNAME, ROLEID, ISEFFECT, WSHCODE, Sex FROM TB_USER ORDER BY NEWID()"; SqlDataAdapter command = new SqlDataAdapter(SQLString, con); command.Fill(ds, "ds"); con.Close(); Program.name = ds.Tables[0].Rows[0]["USERNAME"].ToString(); } catch { Program.name = "无数据源!"; } label1.Text = Program.name;
直接连接*.mdf 文件,去除随机两条记录。