如何以Windows形式将文本框内容传递给sql数据库 [英] how to pass textbox content to sql database in windows form

查看:74
本文介绍了如何以Windows形式将文本框内容传递给sql数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我有4个表单的客户详细信息文本框,希望将按客户键入的数据存储到数据库中,并在数据库中具有表.请举个例子,告诉我如何连接和推送文本框的相应数据.

hi all,

i''ve 4 customer detail text boxes in form and wanted to store data, typed by customer, to database and have table in database. please tell me with an example, how to connect and push the respective data of text boxes.

推荐答案

尝试(这是我的错误日志报告代码,因此您将必须更改名称,依此类推):
Try (this is from my error log reporting code, so you will have to change names, and so forth):
string dbCon = ConfigurationManager.ConnectionStrings[dbConName].ConnectionString;
try
    {
    using (SqlConnection con = new SqlConnection(dbCon))
        {
        con.Open();
        using (SqlCommand com = new SqlCommand("INSERT INTO SMAdmin.Log (message, loggedAt, cause, infoType, additionalInfo) " +
                                               "VALUES (@M, @A, @C, @T, CAST(@I AS VARBINARY(MAX)))", con))
            {
            com.Parameters.AddWithValue("@M", message);
            com.Parameters.AddWithValue("@A", when);
            com.Parameters.AddWithValue("@C", cause);
            com.Parameters.AddWithValue("@T", type);
            if (add != null)
                {
                com.Parameters.AddWithValue("@I", add);
                }
            else
                {
                com.Parameters.AddWithValue("@I", DBNull.Value);
                }
            com.ExecuteNonQuery();
            }
        }
    }
catch
    {
    // Anonymous catch: Error in error reporting system
    // Any attempt to record the problem will most likely makes things worse
    // as the whole system is to c**k anyway.

    }



[edit]为了方便起见,将"message"替换为"textBox1.Text",依此类推... [/edit]



[edit]And just to make it easy for you, replace "message" with "textBox1.Text", and so on...[/edit]


这是一篇不错的文章在数据绑定和Windows窗体上
详细的数据绑定教程 [
Here is a rather good article on databinding and windows forms
A Detailed Data Binding Tutorial[^]

You''ll need to bind the Text property of the text boxes.

Regards
Espen Harlinn


这篇关于如何以Windows形式将文本框内容传递给sql数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆