C#是面向对象的,当你创建了一个连接数据库的类时,必须在调用前实例化,比如: Class1 aaa=new Class1();
不大清楚你为什么要用interface,用CLASS建一个基类不是很好吗

解决方案 »

  1.   

    你的继承自接口的类已经写好了,直接用就可以了。COMLibrary.Class1 c1 = new COMLibrary.Class1();
    System.Object obj = new System.Object();
    if c1.QueryStock(obj,"a string")
    {
    }不过看起来好奇怪,你的这个方法有两个参数,可以都没有使用。所以上面的参数也不知道该传人什么。而且无论什么情况都返回true,实在费解。public bool QueryStock (object reader,string strCommand)
    {
      conConnection.Open(); // 打开数据连接
        OleDbCommand cmd = new OleDbCommand ( strCommand , conConnection ) ;
     return true;
    }
      

  2.   

    public bool QueryStock (object reader,string strCommand)
    {
      conConnection.Open(); // 打开数据连接
        OleDbCommand cmd = new OleDbCommand ( strCommand , conConnection ) ;
     return true;
    }
    这里我是不知道应该怎么写,我的本意是前端传递一个记录集和一个查询字符串或条件,但是怎么也返不回来我把参数改成了public bool QueryStock (out object reader,ref string strCommand)但是也不行能不能告诉我,谢谢解决马上给分.
      

  3.   

    //这是前端的代码
    private void textBox2_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
    {
    if(e.KeyChar == (char)13)
    //设定一个新的网格
    {
    this.dataGrid1.DataMember="";
    DataSet ds=new DataSet();
    //da.Fill(ds);
    //c代表我的组件对象
    //c.reg_no是组件内的一个属性用来给查询语句条件的
    c.reg_no =textBox2.Text;
    //ds是我想传递过去一个记录集对象或返回一个记录集对象好让网格接收数据.可是问题就出在这里返不回来
    c.QueryStock(ds);
    dataGrid1.DataSource=ds.DefaultViewManager;

    }
    }后端的代码
    using System;
    using System.Data.OleDb;
    using System.Data;
    using System.Runtime.InteropServices;//为了调用GUID
    //using System.EnterpriseServices;//引用COM+的类
    //using System.Web.Services;
    [Guid("539448DE-9F3B-4781-A1F6-F3C852091FC9")]
     public interface Query
    {
    bool QueryStock( object reader);
    string reg_no //Property: Name, Get/Set { get; set; }} namespace COMLibrary
      {
    /// <summary>
    /// Class1 的摘要说明。
    /// </summary>
    public class Class1:Query
    {
    private static string strConnect ="Provider=MSDAORA.1;Password=erpii;User ID=erpii;Data Source=erpii;Persist Security Info=True";
    private OleDbConnection conConnection = new OleDbConnection ( strConnect ) ;
    // OleDbDataReader reader ;
    //string strCommand = "SELECT distinct * FROM et_stock_reg_d_iv" ;

    //OleDbCommand cmd = new OleDbCommand ( strCommand , conConnection ) ;
    // reader = cmd.ExecuteReader ( ) ;//获得数据集
    public bool QueryStock ( object reader)
    {
        string StrSql="select * from et_inside_bs where like regno % " + reg_no + "%";
    conConnection.Open(); // 打开数据连接

        OleDbCommand cmd = new OleDbCommand ( StrSql , conConnection ) ;
    //
    //// conConnection.ConnectionString=strConnect;
    // conConnection.Open();
    OleDbDataAdapter da=new OleDbDataAdapter(StrSql,conConnection);

    DataSet ds=new DataSet();
    // da.Fill(ds);
    reader=ds;
    conConnection.Close();
    conConnection.Dispose();
             return true;
    }
    private string mreg_no; public string reg_no //Property: Name, Get/Set { get { return mreg_no; } set { mreg_no = value; } } public Class1()
    {
    //
    // TODO: 在此处添加构造函数逻辑
    //
    } }
    }
    可是怎么才能把记录返回来?那位高手告诉俺重重有分哪!
      

  4.   

    简单举例如下,首先建议接口用I开头,看起来不舒服.1.实现
    public interface IQuery
    {
    bool QueryStock(DataSet myDS); } class DBsource:IQuery
    {
    public bool QueryStock(DataSet myDS)
    { string strConn = "Provider=SQLOLEDB;Data Source=(local);" +
    "Initial Catalog=Northwind;Trusted_Connection=Yes;";
    string StrSql="select * from customers"; OleDbConnection conConnection = new OleDbConnection(strConn);
    conConnection.Open(); // 打开数据连接
    OleDbDataAdapter da = new OleDbDataAdapter(StrSql,conConnection);

    da.Fill(myDS); conConnection.Close();
    conConnection.Dispose();
    return true;
    }
    }
    调用:
    DBsource mydbsource = new DBsource();
    DataSet ds = new DataSet();
    mydbsource.QueryStock(ds); this.dataGrid1.DataSource = ds.DefaultViewManager;