0

我已经使用 SQL Server Management Studio 创建了一个数据库,现在正在尝试使用 Visual Studio Express 2012 编辑该数据库。我已将数据库连接到 Visual Studio 并且可以查看我的数据库,但我无法编辑存储在管理中的数据库使用 Visual Studio 的工作室。我创建了一个表单,并尝试在用户使用 textbox2 和 textbox3 定义列名和行(使用我的数据库中的主键)后,将输入到 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);

编辑

您的连接字符串格式不正确。我相信以下格式适合您当前的需求:

字符串 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 回答