如何才能WCF消耗从数据库phpMyAdmin的数据?消耗、数据库、数据、WCF

2023-09-03 07:05:40 作者:只剩骄傲

很高兴见到大家。我是新在这里了。

只是想问问,怎么能我的WCF使用来自数据库的phpMyAdmin的数据? 我只是想我的WCF消费数据从数据库SQLSERVER和它的作品在我的WPF应用程序。

但我不能找到一种方法,怎么能我的WCF访问数据,如果我的数据库联机。 有没有什么线索?

我尝试更改数据源到IP在我的数据库,这是行不通的。 这里是我的SqlConnection,

 的SqlConnection康恩=新的SqlConnection(数据源=阿尔弗雷德-PC;初始目录=阿尔弗雷德;集成安全性=真);
 

这是WCF

 公共类职位:IJobs
{
    SqlConnection的康恩=新的SqlConnection(数据源=阿尔弗雷德-PC;初始目录=阿尔弗雷德;集成安全性=真);
    SqlDataAdapter的DA;
    数据集DS;
    数据数据=新的Data();
    名单<数据>的ListData =新名单lt;数据>();

    公共数据集详细信息()
    {
        conn.Open();
        DS =新的DataSet();
        DA =新的SqlDataAdapter(选择数据*,康涅狄格州);
        da.Fill(DS);
        conn.Close();
        返回DS;
    }

    公开数据GetDetails(INT作业ID)
    {
        conn.Open();
        DS =新的DataSet();
        DA =新的SqlDataAdapter(选择数据*其中id =+作业ID,康涅狄格州);
        da.Fill(DS);
        如果(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();
        返回的数据;
    }

    公开名单<数据> GetAllDetails()
    {
        conn.Open();
        DS =新的DataSet();
        DA =新的SqlDataAdapter(选择数据*,康涅狄格州);
        da.Fill(DS);
        的foreach(在ds.Tables DataRow中博士[0] .Rows)
        {
            listdata.Add(
                新数据
                {
                    用户ID = Convert.ToInt32(博士[0]),
                    名字=博士[1]的ToString()
                    姓氏=博士[2]的ToString()
                    位置=博士[3]的ToString()
                }
            );
        }
        conn.Close();
        返回的ListData;
    }
}
 

这是WPF文件

 公共部分类主窗口:窗口
{
    公共主窗口()
    {
        的InitializeComponent();
    }

    私人无效的button1_Click(对象发件人,RoutedEventArgs E)
    {
        如果(textbox1.Text.Trim()。持续!= 0)
        {
            ServiceReference1.JobsClient JC =新ServiceReference1.JobsClient();
            变种X = jc.GetDetails(Convert.ToInt32(textbox1.Text));
            如果(x.userid!= 0)
            {
                textbox2.Text = x.userid.ToString();
                textbox3.Text = x.firstname;
                textbox4.Text = x.lastname;
                textbox5.Text = x.location;
            }
            其他
                的MessageBox.show(RecordNotFound ......!,消息,MessageBoxButton.OK,MessageBoxImage.Information);
        }
        其他
            的MessageBox.show(输入用户,消息,MessageBoxButton.OK,MessageBoxImage.Warning);
    }

    私人无效button2_Click(对象发件人,RoutedEventArgs E)
    {
        ServiceReference1.JobsClient JC =新ServiceReference1.JobsClient();
        。dataGrid1.ItemsSource = jc.Details()桌子[0] .DefaultView;
    }

    私人无效button3_Click(对象发件人,RoutedEventArgs E)
    {
        ServiceReference1.JobsClient JC =新ServiceReference1.JobsClient();
        dataGrid1.ItemsSource = jc.GetAllDetails();
    }

}
 
phpmyadmin怎么导入数据库文件

解决方案

如果我理解正确的话,你需要连接字符串的例子。 因此,对于连接字符串格式请看这里: http://www.connectionstrings.com/mysql

一个例子是服务器= myServerAddress;数据库= MyDatabase的; UID =名为myUsername; PWD = MYPASSWORD; 如果您使用的是标准的端口,并没有改变它。在这种情况下,你不会使用集成的安全性,但(你会指定在连接字符串中的用户名和密码),以便检查,如果这是可能的你。

然后你可以在这里的 http://dev.mysql.com/downloads/dotnet.html ),并连接到MySQL数据库编程的详细解释这里的 http://www.functionx.com/mysqlnet/csharp/Lesson02.htm

nice to meet you all. I'm new in here.

Just want to ask, how can my WCF consume data from database phpmyadmin? I just tried my WCF consume data from database sqlserver and it works in my wpf app.

But I can't find a way how can my WCF access the data if my database is online. is there any clue?

i try to change the data source into the IP at my database, it doesn't work. here is my SqlConnection,

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

This is the 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;
    }
}

this is the WPF file

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() ;
    }

}

解决方案

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