C#MySQLDataReader读取强制类型不能转换是怎么回事?
usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Da...
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
//using MySql.Data.MysqlClient;
using MySQLDriverCS;
namespace errorprovider
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
listView1.View = View.Details;
listView1.FullRowSelect = true;
MySQLConnection mc = new MySQLConnection();
mc.ConnectionString = @"Server=localhost;User Id=root;Password=123;Persist Security Info=True;Database=xu";
mc.Open();
MySQLCommand msc = new MySQLCommand();
msc.CommandText = "select * from xuwu";
msc.Connection = mc;
MySQLDataReader md ;
md = (MySQLDataReader)msc.ExecuteReader(); //标记
listView1.Items.Clear();
while (md.Read())
{
ListViewItem lt = new ListViewItem(md.GetValue(4).ToString());
lt.SubItems.Add(md.GetValue(0).ToString());
lt.SubItems.Add(md.GetValue(1).ToString());
lt.SubItems.Add(md.GetValue(2).ToString());
lt.SubItems.Add(md.GetValue(3).ToString());
listView1.Items.Add(lt);
}
md.Close();
mc.Close();
listView1.Alignment = ListViewAlignment.SnapToGrid;
listView1.GridLines = true;
}
private void button2_Click(object sender, EventArgs e)
{
listView1.Items.Clear();
}
private void button3_Click(object sender, EventArgs e)
{
this.Close();
}
}
}
运行后出现如下错误:
F:\C#\errorprovider\errorprovider\Form1.cs(33,18): 错误 CS0266: 无法将类型“System.Data.Common.DbDataReader”隐式转换为“MySQLDriverCS.MySQLDataReader”。存在一个显式转换(是否缺少强制转换?)
后来将上段代码标记部分强制类型转换md = (MySQLDataReader)msc.ExecuteReader();可以运行,但是又出现新问题,请问,这是什么原因啊?我刚学C#帮下忙,谢谢了!!
ExecuteReader()方法的返回值类型要怎么转换?能说清楚一点吗? 展开
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
//using MySql.Data.MysqlClient;
using MySQLDriverCS;
namespace errorprovider
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
listView1.View = View.Details;
listView1.FullRowSelect = true;
MySQLConnection mc = new MySQLConnection();
mc.ConnectionString = @"Server=localhost;User Id=root;Password=123;Persist Security Info=True;Database=xu";
mc.Open();
MySQLCommand msc = new MySQLCommand();
msc.CommandText = "select * from xuwu";
msc.Connection = mc;
MySQLDataReader md ;
md = (MySQLDataReader)msc.ExecuteReader(); //标记
listView1.Items.Clear();
while (md.Read())
{
ListViewItem lt = new ListViewItem(md.GetValue(4).ToString());
lt.SubItems.Add(md.GetValue(0).ToString());
lt.SubItems.Add(md.GetValue(1).ToString());
lt.SubItems.Add(md.GetValue(2).ToString());
lt.SubItems.Add(md.GetValue(3).ToString());
listView1.Items.Add(lt);
}
md.Close();
mc.Close();
listView1.Alignment = ListViewAlignment.SnapToGrid;
listView1.GridLines = true;
}
private void button2_Click(object sender, EventArgs e)
{
listView1.Items.Clear();
}
private void button3_Click(object sender, EventArgs e)
{
this.Close();
}
}
}
运行后出现如下错误:
F:\C#\errorprovider\errorprovider\Form1.cs(33,18): 错误 CS0266: 无法将类型“System.Data.Common.DbDataReader”隐式转换为“MySQLDriverCS.MySQLDataReader”。存在一个显式转换(是否缺少强制转换?)
后来将上段代码标记部分强制类型转换md = (MySQLDataReader)msc.ExecuteReader();可以运行,但是又出现新问题,请问,这是什么原因啊?我刚学C#帮下忙,谢谢了!!
ExecuteReader()方法的返回值类型要怎么转换?能说清楚一点吗? 展开
2个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询