如何将ms-botframework连接到azure数据库

时间:2016-08-08 07:02:55

标签: .net azure-sql-database botframework

如何将.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();
}

1 个答案:

答案 0 :(得分:0)

谢谢大家,我没有为我的问题找到解决方案并重新创建所有示例项目并且它的工作。

本问题中的示例代码

我认为this topic必须关闭