本文介绍了如何从3层的sqldatareader获取一行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 大家好 我遇到了问题。我想知道如何从3层的sqldatareader获取一行? 我的代码不运行。你能告诉我为什么不对吗? 这是我的代码: DAL: public static SqlDataReader Clerk_Search( string name, string family, string melliCode) { SqlDataReader sqlDataReader; SqlCommand comm = new SqlCommand( Clerk_Search ,Data_Access.conn); comm.CommandType = CommandType.StoredProcedure; comm.Parameters.AddWithValue( @ clk_Name,name); comm.Parameters.AddWithValue( @ clk_Family,family); comm.Parameters.AddWithValue( @ clk_MeliCod,melliCode); conn.Open(); sqlDataReader = comm.ExecuteReader(); conn.Close(); return sqlDataReader; } BL: static public System.Data.SqlClient.SqlDataReader Clerk_Search( string name, string family, string meliCode) { System.Data.SqlClient.SqlDataReader sqlDataReader; sqlDataReader = Data_Access.Clerk_Search(name,family,meliCode); return sqlDataReader; } UI private void Clerk_Search_Click( object sender,EventArgs e) { // System.Data.SqlClient。 SqlDataReader sqlDataReader; System.Data.SqlClient.SqlDataReader red; red = Business.Clerk_Search(txtNameClerk.Text.Trim(),txtFamilyClerk.Text.Trim(),txtShomarehyeMeliClerk.Text.Trim()); txtClerkId.Text = red [ clk_Id]。ToString(); } 解决方案 BL: static public string Clerk_Search( string name, string family, string meliCode) { System.Data.SqlClient.SqlDataReader sqlDataReader; sqlDataReader = Data_Access.Clerk_Search(name,family,meliCode); // 你应该使用 // 这将读取一行 sqlDataReader.Read(); // 知道将数据分配给字符串例如 string str = sqlDataReader [ name]的ToString(); str + = : + sqlDataReader [ family]。ToString(); str + = : + sqlDataReader [ meliCode]。ToString(); // 并在UI处接收str并使用以下内容拆分字符串:并获取所需数据 return str; } 或 UI private void Clerk_Search_Click( object sender,EventArgs e) { // System.Data.SqlClient.SqlDataReader sqlDataReader; System.Data.SqlClient.SqlDataReader red; red = Business.Clerk_Search(txtNameClerk.Text.Trim(),txtFamilyClerk.Text.Trim(),txtShomarehyeMeliClerk.Text.Trim()); red.Read(); if (red.HasRow) { txtClerkId.Text = red [ clk_Id]。ToString(); } Hi everyoneI've had a problem. I want to know how to get a row from sqldatareader in 3-tier?My code don't run. Could you tell me Why it isn't correct ?this is my code:DAL: public static SqlDataReader Clerk_Search(string name, string family, string melliCode) { SqlDataReader sqlDataReader; SqlCommand comm = new SqlCommand("Clerk_Search", Data_Access.conn); comm.CommandType = CommandType.StoredProcedure; comm.Parameters.AddWithValue("@clk_Name", name); comm.Parameters.AddWithValue("@clk_Family", family); comm.Parameters.AddWithValue("@clk_MeliCod", melliCode); conn.Open(); sqlDataReader = comm.ExecuteReader(); conn.Close(); return sqlDataReader; }BL:static public System.Data.SqlClient.SqlDataReader Clerk_Search(string name, string family, string meliCode) { System.Data.SqlClient.SqlDataReader sqlDataReader; sqlDataReader = Data_Access.Clerk_Search(name, family, meliCode); return sqlDataReader; }UI private void Clerk_Search_Click(object sender, EventArgs e) { //System.Data.SqlClient.SqlDataReader sqlDataReader; System.Data.SqlClient.SqlDataReader red; red = Business.Clerk_Search(txtNameClerk.Text.Trim(), txtFamilyClerk.Text.Trim(), txtShomarehyeMeliClerk.Text.Trim()); txtClerkId.Text = red["clk_Id"].ToString(); } 解决方案 BL:static public string Clerk_Search(string name, string family, string meliCode){System.Data.SqlClient.SqlDataReader sqlDataReader;sqlDataReader = Data_Access.Clerk_Search(name, family, meliCode);//you shoud read data here using //this will read one line sqlDataReader.Read();//know assing data to string for examplestring str=sqlDataReader["name"].ToString();str+=":"+sqlDataReader["family"].ToString();str+=":"+sqlDataReader["meliCode"].ToString();//and recieve str at UI and split string with : and get required datareturn str;}or in UIprivate void Clerk_Search_Click(object sender, EventArgs e){//System.Data.SqlClient.SqlDataReader sqlDataReader;System.Data.SqlClient.SqlDataReader red;red = Business.Clerk_Search(txtNameClerk.Text.Trim(), txtFamilyClerk.Text.Trim(), txtShomarehyeMeliClerk.Text.Trim());red.Read();if(red.HasRow){ txtClerkId.Text = red["clk_Id"].ToString(); } 这篇关于如何从3层的sqldatareader获取一行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-19 08:56