none
Write data from textbox to database C# RRS feed

  • 问题

  • Hello Guys,

    I just cant figure it out, I've read topics on MSDN which are related to this threat but i cant find the solution.

    So here's the deal: I'm working on a school project which would be a customer system. the department sales is allowed to add new customers to the system, this happens thourgh a form that must be filled. It concludes the fields which are also in the database, so the user fils in the textboxes and than presses "add customer". Than the information should been send to the database. Thats where I'm stuck. I've got no errors nor warnings. But the data wont appear in the database. Please help me out!:( I would be so gratefull.

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    using System.Data.SqlClient;
    using System.Data.SqlServerCe;
    using System.Data.Sql;
    using System.Reflection;
    namespace WindowsFormsApplication3
    {
        public partial class Form2 : Form
        {
            public Form2()
            {
                InitializeComponent();
            }

            private void button1_Click(object sender, EventArgs e)
            {
                addcustomer.Visible = true;

            }

            private void Form2_Load(object sender, EventArgs e)
            {
                addcustomer.Visible = false;
            }

            private void button2_Click(object sender, EventArgs e)
            {
                insertinfo();
            }
            public void insertinfo()
            {
                string appPath = System.IO.Path.GetDirectoryName(Assembly.GetAssembly(typeof(Form1)).CodeBase).Replace(@"file:\", "") + @"\";

                string connection1 = @"Data Source=" + appPath + "Database1.sdf";

                SqlCeConnection connection = new SqlCeConnection(connection1);

                connection.Open();
                // string sql = "INSERT into Customers (company_name,adress1,postcode1 )" + " VALUES (@company_name,@adress1,@postcode1)";

                 SqlCeCommand cmd = new SqlCeCommand("INSERT into Customers (company_name,adress1,postcode1 ) VALUES (@company_name,@adress1,@postcode1)", connection);

                cmd.CommandType = CommandType.Text;
               // cmd.CommandText = sql;

               cmd.Parameters.AddWithValue("@company_name", company_name.Text);

               cmd.Parameters.AddWithValue("@adress1", adress1.Text);

               cmd.Parameters.AddWithValue("@postcode1", postcode1.Text);

             
              
               cmd.ExecuteNonQuery();
               connection.Close();
               }

        

    }

            }
        Regards,

    2013年2月24日 11:27