很高兴见到你们。我是新来的。
我只想问一下,我的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() ;
}
}发布于 2012-06-17 23:03:23
如果我理解正确的话,您需要一个连接字符串的示例。因此,有关连接字符串的格式,请查看此处:http://www.connectionstrings.com/mysql
如果您使用的是标准端口且未更改,则可以使用Server=myServerAddress;Database=myDataBase;Uid=myUsername;Pwd=myPassword;作为示例。在这种情况下,您将不会使用集成安全性(您将在连接字符串中指定用户名和密码),因此请检查您是否可以使用集成安全性。
然后,您可以使用MySQL连接器(可在此处http://dev.mysql.com/downloads/dotnet.html下载命名空间MySql.Data.MySqlClient ),并按照此处详细说明的方式以编程方式连接到MySQL数据库http://www.functionx.com/mysqlnet/csharp/Lesson02.htm
发布于 2012-06-16 23:27:25
您使用的是集成安全性。在WPF应用程序的情况下,因此用于验证数据库访问的帐户是您登录时所使用的帐户。但在WCF服务中,帐户由承载该帐户的(IIS)服务器的设置控制。您有一些选项:
https://stackoverflow.com/questions/11064300
复制相似问题