how to connect ms-botframework to azure database

半腔热情 提交于 2020-01-05 05:30:40

问题


How to connect .Net application BotFramework to database and store data in MS SQL azure?

I want use stored procedure in db and save sent and recieved messages.

Code Sample This code is work if I used connection string to localdb or local server

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:


Thanks all, I dont find solution for my problem and recreate all sample project and it's work.

Sample code in this Questions it's work

I think this topic must be close



来源:https://stackoverflow.com/questions/38823050/how-to-connect-ms-botframework-to-azure-database

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!