如何将.Net应用程序BotFramework连接到数据库并在MS SQL azure中存储数据?
我想在db中使用存储过程并保存已发送和已接收的消息。
代码示例 如果我使用连接字符串到localdb或本地服务器
,此代码是有效的Webconfig
<connectionStrings>
<add name="[ConnStr]" connectionString="Data Source=[SiteURL].database.windows.net;Initial Catalog=[Name];Persist Security Info=True;User ID=[User];Password=[Password]" providerName="System.Data.SqlClient" />
</connectionStrings>
... MessagesController.cs
[BotAuthentication]
public class MessagesController : ApiController
{
/// <summary>
/// POST: api/Messages
/// Receive a message from a user and reply to it
/// </summary>
public async Task<HttpResponseMessage> Post([FromBody]Activity activity)
{
if (activity.Type == ActivityTypes.Message)
{
ConnectorClient connector = new ConnectorClient(new Uri(activity.ServiceUrl));
// calculate something for us to return
int length = (activity.Text ?? string.Empty).Length;
// return our reply to the user
Activity reply = activity.CreateReply($"You sent {activity.Text} which was {length} characters");
LogMessage();
await connector.Conversations.ReplyToActivityAsync(reply);
}
else
{
HandleSystemMessage(activity);
}
var response = Request.CreateResponse(HttpStatusCode.OK);
return response;
}
...
private void LogMessage()
{
var context = new DataClasses1DataContext(ConfigurationManager.ConnectionStrings["[ConnStr]"].ConnectionString);
var newMessage = new Table1
{
Table101 = newID()
,
Table102 = DateTime.Now
,
Table103 = 0
,
Table104 = "Test"
,
Table105 = "Test2"
,
Table106 = "Test3"
,
Table107 = 0
,
Table108 = "Test4"
,
Table109 = 0
};
context.Table1.InsertOnSubmit(newMessage);
context.Table1.Context.SubmitChanges();
}