从Windows窗体将数据保存在数据库中 [英] saving a data in database from a windows form

查看:93
本文介绍了从Windows窗体将数据保存在数据库中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我是该网站的新手,所以我不知道要使用它,如何答复以及所有内容.
我正在做一个项目,我想用C#中的代码将文本boxex的给定输入从Windows窗体保存到数据库中.
任何人都可以举一个我在本地运行的简单示例.
我将根据我的要求修改代码.

请帮帮我.

在此先谢谢您.

Hi all,

I am a newbie to this site, so I dont know to use this, how to reply and all stuffs.
I am doing a project and I want code in C# for saving a given input from text boxex into a database from a windows form.
Can any one give a simple example that I run at my local.
I will modify the code as per my requirement.

Please help me.

Thanks in advance.

推荐答案

这很简单:
It''s pretty easy:
using (SqlConnection con = new SqlConnection(strConnect))
    {
    con.Open();
    using (SqlCommand com = new SqlCommand("INSERT INTO myTable (myColumn1, myColumn2) VALUES (@C1, @C2)", con))
        {
        com.Parameters.AddWithValue("@C1", myValueForColumn1);
        com.Parameters.AddWithValue("@C2", myValueForColumn2);
        com.ExecuteNonQuery();
        }
    }


我将通过编写代码来帮助您,

I will help you by writing the code,

private void button1_Click(object sender, EventArgs e)
       {
           con.Open();
           cmd = new SqlCommand("Insert into SalesDetails Values('" + textBox1.Text + "','" + dateTimePicker1.Text + "','" + textBox2.Text + "','" + textBox3.Text + "','" + textBox4.Text + "','" + textBox5.Text + "','" + textBox6.Text + "','" + textBox7.Text + "','" + textBox8.Text + "','" + textBox9.Text + "','" + textBox10.Text + "')", con);
           cmd.ExecuteNonQuery();
           con.Close();
           MessageBox.Show("Added");
       }


这篇关于从Windows窗体将数据保存在数据库中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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