尝试通过wcf从数据库中检索记录时出错

时间:2013-03-13 15:29:39

标签: c# mysql winforms wcf

我创建了一个wcf服务并定义了一个从mysql数据库中检索记录的方法。 当我从Windows窗体应用程序调用该方法时,它会抛出异常。它表示没有处理null值。我有两个不同的问题。一个显然是如何处理空值,另一个是该方法不起作用,因为我的数据库中有一条记录,所以它不应该接收空值。

如果我在表单应用程序中定义方法,它可以正常工作,但不能通过WCF服务。我正在使用mysql作为数据库。

Wcf代码如下:

IService1
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using System.Data;

namespace WcfService3
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IService1" in both code and config file together.
    [ServiceContract]
    public interface IService1
    {

       // [OperationContract]
       // string InsertUserDetails(EmpDetails userInfo);

         [OperationContract]
         DataSet SelectEmpDetails();

        // TODO: Add your service operations here
    }


    // Use a data contract as illustrated in the sample below to add composite types to service operations.
    [DataContract]
    public class CompositeType
    {
        bool boolValue = true;
        string stringValue = "Hello ";

        [DataMember]
        public bool BoolValue
        {
            get { return boolValue; }
            set { boolValue = value; }
        }

        [DataMember]
        public string StringValue
        {
            get { return stringValue; }
            set { stringValue = value; }
        }
    }

    [DataContract]
    public class EmpDetails
    {

        int empno;

        string ename;

        float sal;

        int deptno;

        string email;



        [DataMember]

        public int EmpNp
        {

            get { return empno; }

            set { empno = value; }

        }



        [DataMember]

        public string EName
        {

            get { return ename; }

            set { ename = value; }

        }

        [DataMember]

        public float Sal
        {

            get { return sal; }

            set { sal = value; }

        }

        [DataMember]

        public int DeptNo
        {

            get { return deptno; }

            set { deptno = value; }

        }

        [DataMember]

        public string Email
        {

            get { return email; }

            set { email = value; }

        }

    }
}

Service1.svc

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using System.Data;
using MySql.Data.MySqlClient;
using System.Windows.Forms;

namespace WcfService3
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together.
    // NOTE: In order to launch WCF Test Client for testing this service, please select Service1.svc or Service1.svc.cs at the Solution Explorer and start debugging.
    public class Service1 : IService1
    {

        private MySqlConnection connection;
        private string server;
        private string database;
        private string uid;
        private string password;


        //Constructor
    public Service1()
    {
        Initialize();
    }


    private void Initialize()
    {
        server = "192.168.1.222";
        database = "cabee";
        uid = "user";
        password = "Password1";
        string connectionString;
        connectionString = "SERVER=" + server + ";" + "DATABASE=" +
        database + ";" + "UID=" + uid + ";" + "PASSWORD=" + password + ";";

        connection = new MySqlConnection(connectionString);
    }
    private bool OpenConnection()
    {
        try
        {
            connection.Open();
            return true;
        }
        catch (MySqlException ex)
        {
            //When handling errors, you can your application's response based 
            //on the error number.
            //The two most common error numbers when connecting are as follows:
            //0: Cannot connect to server.
            //1045: Invalid user name and/or password.
            switch (ex.Number)
            {
                case 0:
                    MessageBox.Show("Cannot connect to server.  Contact administrator");
                    break;

                case 1045:
                    MessageBox.Show("Invalid username/password, please try again");
                    break;
            }
            return false;
        }
    }

    //Close connection
    private bool CloseConnection()
    {
        try
        {
            connection.Close();
            return true;
        }
        catch (MySqlException ex)
        {
            MessageBox.Show(ex.Message);
            return false;
        }
    }

        public string GetData(int value)
        {
            return string.Format("You entered: {0}", value);
        }

        public CompositeType GetDataUsingDataContract(CompositeType composite)
        {
            if (composite == null)
            {
                throw new ArgumentNullException("composite");
            }
            if (composite.BoolValue)
            {
                composite.StringValue += "Suffix";
            }
            return composite;
        }

        public DataSet SelectEmpDetails()
        {
            DataSet ds = new DataSet();

            if (this.OpenConnection() == true)
            {

                MySqlCommand cmd = new MySqlCommand("Select * from empinfo", connection);

                MySqlDataAdapter sda = new MySqlDataAdapter(cmd);          

                sda.Fill(ds);

                cmd.ExecuteNonQuery();

                this.CloseConnection();

                return ds;
            }
            else
            {
                return ds;

            }
        }

    }

}

Windows窗体应用程序

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MySql.Data.MySqlClient;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {

        private MySqlConnection connection;
        private string server;
        private string database;
        private string uid;
        private string password;

        ServiceReference1.Service1Client obj = new ServiceReference1.Service1Client(); // Add service reference

        public Form1()
        {
                 showdata();
        }



        private void showdata()  // to show the data in the DataGridView

        {

            DataSet   ds = new DataSet();

            ds = obj.SelectEmpDetails();


            dataGridView1.DataSource = ds.Tables[0];

            dataGridView1.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells);

        }



    }
}

错误消息

System.NullReferenceException was unhandled
  HResult=-2147467261
  Message=Object reference not set to an instance of an object.
  Source=WindowsFormsApplication1
  StackTrace:
       at WindowsFormsApplication1.Form1.showdata() in c:\Users\Anup\Google Drive\WindowsFormsApplication1\WindowsFormsApplication1\Form1.cs:line 100
       at WindowsFormsApplication1.Form1..ctor() in c:\Users\Anup\Google Drive\WindowsFormsApplication1\WindowsFormsApplication1\Form1.cs:line 29
       at WindowsFormsApplication1.Program.Main() in c:\Users\Anup\Google Drive\WindowsFormsApplication1\WindowsFormsApplication1\Program.cs:line 19
       at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException: 

2 个答案:

答案 0 :(得分:0)

问题是你是在构造函数之前在类的init上创建服务引用。

服务引用的创建使用配置文件中的值,但此时未加载配置文件。

尝试将这些线放在一起:

private void showdata()  // to show the data in the DataGridView
{

        DataSet   ds = new DataSet();
        ServiceReference1.Service1Client obj = new ServiceReference1.Service1Client(); 

        ds = obj.SelectEmpDetails();

        //.....
}

答案 1 :(得分:0)

使用数据集

时,您不需要cmd.ExecuteNonQuery()

请看以下帖子 Connecting to a Mysql DB with C# - Need some with Datasets