2012-11-28 80 views
0

我已创建使用SQL Server Management Studio中的数据库,我现在尝试编辑使用Visual Studio Express的2012年。我已经连接了数据库,Visual Studio和可以看到我的数据库,数据库,但我无法使用Visual Studio编辑存储在Management Studio中的数据库。我创建了一个表格,我试图插入用户定义的列名和行(使用我的数据库中的主键)与TextBox2中和textbox3以后有什么输入到TextBox1中为特定的细胞上我的数据库。我可以使用什么代码来执行此操作?到目前为止,我没有运气。预先感谢您的帮助。编辑数据库C#

这是我当前的代码:

 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(); 
     } 


    } 
} 
+0

你们是不是要动态地添加数据库列?请告诉我们你到目前为止做了什么。 –

+0

这是我迄今为止执行此任务的代码,但尚未运行。我不想向数据库添加任何额外的行或列,而是使用预先设置的行和列将值输入到数据库的单元格中。 – user1860435

+0

您正在将db列与参数混淆。见下面回答: –

回答

0

值条款应指定一个参数,该参数应该添加参数值时,可以指定。尝试替代以下:

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

编辑的格式不正确

连接字符串。我相信下面的格式是否适合您当前需求:

字符串myConnectionString =服务器= myServerAddress;数据库= 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分配替换连接和命令语句。

+0

谢谢你固定的部分,可惜使用的代码不输入任何数据到我在Management Studio数据库。 – user1860435

+0

我已经修改了我的答案 - 参见上文。 –

+0

我收到以下与throw new Exception(ex.Message)相关的错误;行: “在WindowsFormsApplication1.exe中发生未处理的类型为'System.Exception'的异常 附加信息:”正在执行“附近的语法错误。” – user1860435