[英]Edit database in management studio using visual studio c#
我已经使用SQL Server Management Studio创建了一个数据库,现在正尝试使用Visual Studio Express 2012编辑该数据库。我已将该数据库连接到Visual Studio,可以看到我的数据库,但无法编辑存储在管理中的数据库。 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();
}
}
}
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分配替换连接和命令语句。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.