Javascript 为数据库操作构建WCF服务
我们在某个服务器上有一个数据库:位于上的database1.mdf 我们需要在C上构建一个WCF服务,控制台自动托管将连接到远程数据库,并提供一些功能,如:读取记录、插入新记录、执行“选择”查询等 最后,我们需要在JavaScript上编写客户端页面,该页面将与WCF服务通信并向客户端提供信息 在这个问题上我是新手,已经找到了很多信息,但是使用很多不同的技术、框架、参考资料等似乎都太复杂了。我想要一个非常基本但强大的解决方案,可以使用基本的MVS工具来完成。因此,任何帮助都将不胜感激 以下是我已经做过的: 可以从URL访问数据库:例如,“someserver”可以是localhost 我已经用控制台自动托管构建了一个基本的WCF服务。该程序看起来像:Javascript 为数据库操作构建WCF服务,javascript,c#,sql,database,wcf,Javascript,C#,Sql,Database,Wcf,我们在某个服务器上有一个数据库:位于上的database1.mdf 我们需要在C上构建一个WCF服务,控制台自动托管将连接到远程数据库,并提供一些功能,如:读取记录、插入新记录、执行“选择”查询等 最后,我们需要在JavaScript上编写客户端页面,该页面将与WCF服务通信并向客户端提供信息 在这个问题上我是新手,已经找到了很多信息,但是使用很多不同的技术、框架、参考资料等似乎都太复杂了。我想要一个非常基本但强大的解决方案,可以使用基本的MVS工具来完成。因此,任何帮助都将不胜感激 以下是我已
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ServiceModel;
using System.ServiceModel.Description;
using System.Data.SqlClient;
using System.Data;
namespace hostWCF
{
class Program
{
static void Main(string[] args)
{
ServiceHost myServiceHost = null;
try
{
Uri httpBaseAddress = new Uri("http://localhost:4321/myService");
myServiceHost = new ServiceHost(typeof(myService.myService), httpBaseAddress);
myServiceHost.AddServiceEndpoint(typeof(myService.ImyService), new WSHttpBinding(), "");
ServiceMetadataBehavior serviceBehavior = new ServiceMetadataBehavior();
serviceBehavior.HttpGetEnabled = true;
myServiceHost.Description.Behaviors.Add(serviceBehavior);
myServiceHost.Open();
Console.WriteLine("myService was launched at: {0}", httpBaseAddress);
Console.WriteLine();
// DO WE NEED TO MAKE A CONNECTION WITH DATABASE HERE?
Console.ReadKey();
}
catch (Exception ex)
{
myServiceHost = null;
Console.WriteLine("myService has faced a problem: " + ex.Message);
Console.ReadKey();
}
}
}
}
启动服务后,如何连接到远程数据库?还是将连接过程作为服务函数放入ImyService[操作合同]中更好?也许最好不要一直保持连接,而是在客户端与数据库通信时才打开连接?那么,如何提供对数据库执行查询的方法呢?最后,如何编写一个代理类,将JSON格式的数据发送到客户端页面
我发现这两个教程对第一个教程中的数据库方法和第二个教程中的控制台主机训练非常有用,但我不想像第一个教程中那样使用ASP.NET Web应用程序。因此,我想知道如何将基于*.svc的WFC数据服务添加到第二个解决方案中。。可以使用代码向主机添加服务引用,因为只有在主机运行时才能找到服务。MVS的“添加服务引用…”没有看到它
更新:
我又往前走了一点。现在,我的WCF控制台主机应用程序使用本地数据库看起来非常简单
using System;
using System.ServiceModel;
using System.Data;
using System.Data.SqlClient;
namespace demoWCFHost
{
public class Program
{
public static void Main()
{
BasicHttpBinding binding = new BasicHttpBinding();
binding.Name = "binding1";
Uri baseAddress = new Uri(@"http://localhost:8000/service1");
ServiceHost serviceHost = new ServiceHost(typeof(Service1), baseAddress);
serviceHost.AddServiceEndpoint(typeof(IService1), binding, baseAddress);
serviceHost.Open();
Console.WriteLine("The service is ready.");
Console.WriteLine("Press <ENTER> to terminate service.");
Console.WriteLine();
Console.ReadLine();
serviceHost.Close();
}
}
[ServiceContract]
public interface IService1
{
[OperationContract]
string DoWork();
}
[ServiceBehavior(IncludeExceptionDetailInFaults = true)]
public class Service1 : IService1
{
public string DoWork()
{
Console.WriteLine("DoWork() method was called");
string connectionString = @"data source=(LocalDB)\v11.0;attachdbfilename=|DataDirectory|\database1.mdf;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework";
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
Console.WriteLine("database is connected");
Console.WriteLine();
string response = String.Empty;
using (SqlCommand command = new SqlCommand("SELECT * FROM table", connection))
{
using (SqlDataReader reader = command.ExecuteReader())
{
while (reader.Read())
{
for (int i = 0; i < reader.FieldCount; i++)
{
response += reader.GetValue(i);
}
}
}
}
connection.Close();
Console.WriteLine("database is disconnected");
Console.WriteLine();
return response;
}
}
}
}
关于如何修复它有什么想法吗?此外,我们仍然需要使数据库连接远程而不是本地
<html>
<head>
<script language="javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.3/jquery.min.js"></script>
</head>
<body>
<div id="results"></div>
<script language="javascript">
$(
function () {
$.ajax( {
type: 'POST',
url: 'http://localhost:8000/service1',
data: '<'+'soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">'+
'<'+'soap:Body>'+
'<'+'DoWork xmlns="http://tempuri.org/">'+
'<'+'/DoWork>'+
'<'+'/soap:Body>'+
'<'+'/soap:Envelope>',
success: function (data, textStatus, xhr) {
$('#results').html(escape(xhr.responseText));
},
dataType: 'xml'
});
});
</script>
</body>
</html>
POST http://localhost:8000/service1 jquery.min.js:140
ajax @ jquery.min.js:140
(anonymous function) @ (index):52
ready @ jquery.min.js:28
t @ jquery.min.js:36
(index):1
XMLHttpRequest cannot load http://localhost:8000/service1. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost' is therefore not allowed access. The response had HTTP status code 415.