【问题标题】:how to connect ms-botframework to azure database如何将 ms-botframework 连接到 azure 数据库
【发布时间】:2016-08-31 08:24:20
【问题描述】:

如何将.Net应用程序BotFramework连接到数据库并将数据存储在MS SQL azure中?

我想在数据库中使用存储过程并保存发送和接收的消息。

代码示例 如果我使用到 localdb 或本地服务器的连接字符串,则此代码有效

网络配置

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

【问题讨论】:

  • 你能告诉我们你到目前为止所做的尝试吗?
  • 感谢您的回复。我添加了一些代码示例
  • 您在连接到 Azure SQL 数据库时是否遇到任何错误?
  • 谢谢你的回复,你帮了我

标签: .net azure-sql-database botframework


【解决方案1】:

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

此问题中的示例代码有效

我认为this topic 一定很接近

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-08-17
    • 2013-02-12
    • 1970-01-01
    • 2018-01-30
    • 2017-12-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多