我已创建使用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();
}
}
}
你们是不是要动态地添加数据库列?请告诉我们你到目前为止做了什么。 –
这是我迄今为止执行此任务的代码,但尚未运行。我不想向数据库添加任何额外的行或列,而是使用预先设置的行和列将值输入到数据库的单元格中。 – user1860435
您正在将db列与参数混淆。见下面回答: –