无法将类型“System.Data.SqlClient.SqlDataReader”隐式转换为“System.Data.SqlClient.SqlDataAd

Songmaolan 2009-11-28 03:21:54
public static SqlDataAdapter ExSQLReDr(string SQLStr)
{
SqlConnection cnn = new SqlConnection(Constr);
try
{

SqlCommand cmd = new SqlCommand(SQLStr, cnn);
cnn.Open();
SqlDataReader dataReader = cmd.ExecuteReader();

return dataReader;
}
catch (Exception e)
{

return null;
}
finally
{
cnn.Close();
}
}
我写了上面的代码,在运行过程中出现下面的错误
无法将类型“System.Data.SqlClient.SqlDataReader”隐式转换为“System.Data.SqlClient.SqlDataAdapter”
并且在调用该函数时,出现ExSQLReDr函数没有定义的错误!请大家帮忙看看错误出现在哪?
...全文
330 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
Songmaolan 2009-11-29
  • 打赏
  • 举报
回复
问题已经解决了!谢谢大家的帮助
cuike519 2009-11-28
  • 打赏
  • 举报
回复
返回类型和函数体中返回的类型不一致,无法转换。


楼主肯定没有编译项目,这个错误在编译时是可以检查出来的,你这个错误应该是在运行时编译器发出的错误,话说运行时的编译器就是牛,还尝试帮你转换类型呢。。。
whowhen21 2009-11-28
  • 打赏
  • 举报
回复
方法声明时候写的返回SqlDataAdapter,而方法实际的返回却是SqlDataReader,当然不能转换呀。
public static SqlDataAdapter ExSQLReDr(string SQLStr)
{
SqlConnection cnn = new SqlConnection(Constr);
try
{

SqlCommand cmd = new SqlCommand(SQLStr, cnn);
cnn.Open();
SqlDataReader dataReader = cmd.ExecuteReader();
return dataReader;

}
catch (Exception e)
{

return null;
}
finally
{
cnn.Close();
}
}
whowhen21 2009-11-28
  • 打赏
  • 举报
回复
方法声明时候写的返回SqlDataAdapter,而方法实际的返回却是SqlDataReader,当然不能转换呀。

public static ExSQLReDr(string SQLStr)
{
SqlConnection cnn = new SqlConnection(Constr);
try
{

SqlCommand cmd = new SqlCommand(SQLStr, cnn);
cnn.Open();
SqlDataReader dataReader = cmd.ExecuteReader();
return dataReader;
}
catch (Exception e)
{

return null;
}
finally
{
cnn.Close();
}
}
mohugomohu 2009-11-28
  • 打赏
  • 举报
回复
public static SqlDataReader ExSQLReDr(string SQLStr)
你说的是这个吗
qqzeng-ip 2009-11-28
  • 打赏
  • 举报
回复
SqlDataAdapter 互转 SqlDataReader
企业客户资源管理系统 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Data.SqlClient; namespace CRM { public partial class frmLogin : Form { public static string M_str_name;//记录登录用户名字 public static string M_str_pwd;//记录登录用户密码 public static string M_str_right;//记录登录用户的权限 CRM.BaseClass.BaseOperate boperate = new CRM.BaseClass.BaseOperate(); CRM.BaseClass.OperateAndValidate opAndvalidate = new CRM.BaseClass.OperateAndValidate(); public frmLogin() { InitializeComponent(); } private void frmLogin_Load(object sender, EventArgs e) { opAndvalidate.cboxBind(\"select UserName from tb_User\", \"tb_User\", \"UserName\", cboxUName); } private void cboxUName_SelectedIndexChanged(object sender, EventArgs e) { SqlDataReader sqlread = boperate.getread(\"select UserName,UserRight from tb_User where UserName=\'\" + cboxUName.Text + \"\'\"); if (sqlread.Read()) { labURight.Text = sqlread[\"UserRight\"].ToString(); M_str_right = labURight.Text; } sqlread.Close(); } private void btnLogin_Click(object sender, EventArgs e) { SqlDataReader sqlread=boperate.getread(\"select UserName,UserPwd from tb_User where UserName=\'\"+cboxUName.Text.Trim()+\"\' and UserPwd=\'\"+txtPwd.Text.Trim()+\"\'\"); sqlread.Read(); if (sqlread.HasRows) { M_str_name = cboxUName.Text; M_str_pwd = txtPwd.Text.Trim(); frmMain fmain = new frmMain(); this.Hide(); fmain.Show(); } else { MessageBox.Show(\"用户名或密码错误!\", \"提示\", MessageBoxButtons.OK, MessageBoxIcon.Information); txtPwd.Text = \"\"; cboxUName.Focus(); } sqlread.Close(); } private void btnExit_Click(object sender, EventArgs e) { Application.Exit(); } private void frmLogin_FormClosing(object sender, FormClosingEventArgs e) { Application.Exit(); } } }
VB与sql设计代码: Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click '选择普通用户登录 If Me.RadioButton1.Checked Then Dim str As String str = "data source=PC-201103171305\SQLEXPRESS;database=学生信息库;user id=sa;pwd=" Dim con As New SqlClient.SqlConnection(str) Try con.Open() Catch ex As Exception End Try Dim sql As String = "select * from 普通用户where 用户名='" & TextBox1.Text.ToString().Trim() & "' and 密码='" & TextBox2.Text.ToString().Trim() & " '" Dim cmd As New SqlClient.SqlCommand(sql, con) Dim reader As SqlClient.SqlDataReader Try reader = cmd.ExecuteReader If reader.Read() = True Then Dim 学生成绩管理系统As New 学生信息管理系统 学生成绩管理系统.添加DToolStripMenuItem.Enabled = False 学生成绩管理系统.修改KToolStripMenuItem.Enabled = False 学生成绩管理系统.删除DToolStripMenuItem.Enabled = False 学生成绩管理系统.添加ToolStripMenuItem.Enabled = False 学生成绩管理系统.高级用户信息管理ToolStripMenuItem.Enabled = False 学生成绩管理系统.Show() Me.Hide() Else MsgBox("对不起,用户名或密码错误!重新输入(请正确选择用户类型)", 16, "警告") Me.TextBox1.Text = "" Me.TextBox2.Text = "" TextBox1.Focus() End If Catch ex As Exception End Try End If '选择高级用户登录 If Me.RadioButton2.Checked Then Dim str As String str = "data source=PC-201103171305\SQLEXPRESS;database=学生信息库;user id=sa;pwd=" Dim con As New SqlClient.SqlConnection(str) Try con.Open() Catch ex As Exception End Try Dim sql As String = "select * from 高级用户where 用户名='" & TextBox1.Text.ToString().Trim() & "

110,536

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧