1

荣幸和你们见面。我是新来的。

只是想问一下,我的 WCF 如何使用数据库 phpmyadmin 中的数据?我刚刚尝试了我的 WCF 使用数据库 sqlserver 中的数据,它在我的 wpf 应用程序中工作。

但是如果我的数据库在线,我找不到我的 WCF 如何访问数据的方法。有什么线索吗?

我尝试将数据源更改为我的数据库中的 IP,它不起作用。这是我的SqlConnection,

SqlConnection conn = new SqlConnection("Data Source=Alfred-PC;Initial Catalog=alfred;Integrated Security=True");

这是 WCF

public class Jobs : IJobs
{
    SqlConnection conn = new SqlConnection("Data Source=Alfred-PC;Initial Catalog=alfred;Integrated Security=True");
    SqlDataAdapter da;
    DataSet ds;
    Data data = new Data();
    List<Data> listdata = new List<Data>();

    public DataSet Details()
    {
        conn.Open();
        ds = new DataSet();
        da = new SqlDataAdapter("Select * from data", conn);
        da.Fill(ds);
        conn.Close();
        return ds;
    }

    public Data GetDetails(int jobid)
    {
        conn.Open();
        ds = new DataSet();
        da = new SqlDataAdapter("Select * from data where id = " + jobid, conn);
        da.Fill(ds);
        if (ds.Tables[0].Rows.Count > 0)
        {
            data.userid = Convert.ToInt32(ds.Tables[0].Rows[0][0]);
            data.firstname = ds.Tables[0].Rows[0][1].ToString();
            data.lastname = ds.Tables[0].Rows[0][2].ToString();
            data.location = ds.Tables[0].Rows[0][3].ToString();
            ds.Dispose();
        }
        conn.Close();
        return data;
    }

    public List<Data> GetAllDetails()
    {
        conn.Open();
        ds = new DataSet();
        da = new SqlDataAdapter("Select * from data", conn);
        da.Fill(ds);
        foreach (DataRow dr in ds.Tables[0].Rows)
        {
            listdata.Add(
                new Data
                {
                    userid = Convert.ToInt32(dr[0]),
                    firstname = dr[1].ToString(),
                    lastname = dr[2].ToString(),
                    location = dr[3].ToString()
                }
            );
        }
        conn.Close();
        return listdata;
    }
}

这是 WPF 文件

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, RoutedEventArgs e)
    {
        if (textbox1.Text.Trim().Length != 0)
        {
            ServiceReference1.JobsClient jc = new ServiceReference1.JobsClient();
            var x = jc.GetDetails(Convert.ToInt32(textbox1.Text));
            if (x.userid != 0)
            {
                textbox2.Text = x.userid.ToString();
                textbox3.Text = x.firstname;
                textbox4.Text = x.lastname;
                textbox5.Text = x.location;
            }
            else
                MessageBox.Show("RecordNotFound ... !", "Message", MessageBoxButton.OK, MessageBoxImage.Information);
        }
        else
            MessageBox.Show("EnterID", "Message", MessageBoxButton.OK, MessageBoxImage.Warning);
    }

    private void button2_Click(object sender, RoutedEventArgs e)
    {
        ServiceReference1.JobsClient jc = new ServiceReference1.JobsClient();
        dataGrid1.ItemsSource = jc.Details().Tables[0].DefaultView;
    }

    private void button3_Click(object sender, RoutedEventArgs e)
    {
        ServiceReference1.JobsClient jc = new ServiceReference1.JobsClient();
        dataGrid1.ItemsSource = jc.GetAllDetails() ;
    }

}
4

2 回答 2

1

您正在使用集成安全性。对于 WPF 应用程序,用于验证对数据库的访问权限的帐户是您登录的帐户。但在 WCF 服务中,该帐户由托管它的 (IIS) 服务器的设置控制。你有一些选择:

  1. 更改为使用用户名和密码连接到数据库
  2. Change the DB to accept the account that the WCF service is trying to connect as (the DB should tell you in an error message or log)
  3. Change the server settings to use a Windows account that has privileges
于 2012-06-16T15:27:24.953 回答
1

If I understand correctly you need an example for the connection string. So for the connection string format please look here: http://www.connectionstrings.com/mysql

An example could be Server=myServerAddress;Database=myDataBase;Uid=myUsername;Pwd=myPassword; if you are using the standard port and did not change it. In this case you won't use integrated security though (you will specify the username and password in the connection string) so check if that is possible for you.

Then you may use the MySQL Connector (Namespace MySql.Data.MySqlClient downloadable here http://dev.mysql.com/downloads/dotnet.html) and connect to the MySQL database programatically as explained in detail here http://www.functionx.com/mysqlnet/csharp/Lesson02.htm

于 2012-06-17T15:03:22.893 回答