0

SQL Server Management Studio を使用してデータベースを作成し、Visual Studio Express 2012 を使用してそのデータベースを編集しようとしています。データベースを Visual Studio に接続し、データベースを表示できますが、Management に保存されているデータベースを編集できませんでした。 Visual Studio を使用するスタジオ。フォームを作成し、ユーザーが textbox2 と textbox3 を使用して (DB の主キーを使用して) 列名と行を定義した後、textbox1 に入力された内容をデータベースの特定のセルに挿入しようとしています。このアクションを実行するためにどのコードを使用できますか? これまでのところ、私は運がありませんでした。よろしくお願いします。

これは私の現在のコードです:

      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.Sql;
using Microsoft.SqlServer.Server;
using System.Data.Linq;
using System.Data.Linq.Mapping;



namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        SqlConnection myConnectionString = new SqlConnection("Data Source=Server Catalog=DataBase;Integrated Security=True");
        SqlCommand command = new SqlCommand();

        private void maskedTextBox1_MaskInputRejected(object sender, MaskInputRejectedEventArgs e)
        {

        }

        private void button3_Click(object sender, EventArgs e)
        {
            try
        {
            using (SqlConnection dbConnection = new SqlConnection()) // the "using" construct will close and dispose of the connection 
            {
                dbConnection.ConnectionString = ("Data Source=Server ;Initial Catalog=Database;Integrated Security=True");
                dbConnection.Open();
                maskedTextBox1.Clear();
                dateTimePicker1.Value = DateTime.Now.AddDays(0);
                comboBox1.SelectedIndex = -1;

                String username = comboBox2.Text;
                using (SqlCommand command = new SqlCommand("INSERT INTO [Table Name] (Column Name) VALUES ([Parm1])", dbConnection))
                {
                    command.Parameters.AddWithValue("Parm1", username);
                    command.ExecuteNonQuery();
                }
            }
        }
        catch (Exception ex)
        {
            throw new Exception(ex.Message);
        }
      }      


        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            //Close the Window
            this.Close();
        }

        private void label4_Click(object sender, EventArgs e)
        {

        }

        private void comboBox1_DataSourceChanged(object sender, EventArgs e)
        {

        }

        private void button4_Click(object sender, EventArgs e)
        {
            myConnectionString.Open();
            MessageBox.Show("Sql is connected");
            myConnectionString.Close();
        }


    }
}
4

1 に答える 1

0

VALUES句はパラメーターを指定する必要があり、パラメーター値を追加するときにそのパラメーターを指定する必要があります。次のように置き換えてみてください。

String sqlquery = "INSERT INTO [Man Power] ([Person_Performing_Phase_2]) VALUES ([Parm1])";
SqlCommand command = new SqlCommand(sqlquery, con);
command.Parameters.AddWithValue("Parm1", username);

編集

接続文字列が正しく形成されていません。次の形式が現在のニーズに適していると思います。

String myConnectionString = Server = myServerAddress; Database = myDataBase; Trusted_Connection = True;

次のコードを使用してデータを挿入します。これにより、各操作の接続が開閉されます。「command.ExecuteNonQuery」ステートメントに注意してください。接続を開いてもエラーがスローされなかった理由はわかりませんが、その省略が挿入が機能しなかった理由です。

     private void button3_Click(object sender, EventArgs e)
     {
        try
        {
            using (SqlConnection dbConnection = new SqlConnection()) // the "using" construct will close and dispose of the connection 
            {
                dbConnection.ConnectionString = myConnectionString;
                dbConnection.Open();
                maskedTextBox1.Clear();
                dateTimePicker1.Value = DateTime.Now.AddDays(0);
                comboBox1.SelectedIndex = -1;

                String username = comboBox2.Text;
                using (SqlCommand command = new SqlCommand("INSERT INTO [Man Power] ([Person_Performing_Phase_2]) VALUES ([Parm1])", dbConnection))
                {
                    command.Parameters.AddWithValue("Parm1", username);
                    command.ExecuteNonQuery();
                }
            }
        }
        catch (Exception ex)
        {
            throw new Exception(ex.Message);
        }
      }

また、他のすべての_Clickメソッドを削除し、接続とコマンドステートメントをmyConnectionString割り当てに置き換える必要があります。

于 2012-11-28T16:47:31.183 に答える