C#-插入SQL Server而不进行更改
C#的新成员 我正在尝试使用windows窗体执行一个简单的INSERT SQL语句。 应该发生的是,当按下按钮时,它将执行SQL语句,但当我按下按钮时,什么也不会发生 有什么我遗漏的吗C#-插入SQL Server而不进行更改,c#,sql-server,C#,Sql Server,C#的新成员 我正在尝试使用windows窗体执行一个简单的INSERT SQL语句。 应该发生的是,当按下按钮时,它将执行SQL语句,但当我按下按钮时,什么也不会发生 有什么我遗漏的吗 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text;
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;
namespace windowFormsApp_test
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
// TODO: This line of code loads data into the 'por_testDataSet.tbl_persons' table. You can move, or remove it, as needed.
this.tbl_personsTableAdapter.Fill(this.por_testDataSet.tbl_persons);
}
private void button1_Click(object sender, EventArgs e)
{
string connectionString = "Data Source=DC-POR\\SQLEXPRESS;Initial Catalog=por_test;Integrated Security=True;";
SqlConnection con = new SqlConnection(connectionString);
con.Open();
string sql = "INSERT into tbl_persons (person_id,p_name,p_surname) VALUES (55,'TEST','test')";
SqlCommand cmd = new SqlCommand(sql);
con.Close();
}
您需要调用
ExecuteNonQuery
来实际执行插入操作
代码应如下所示:
private void button1_Click(object sender, EventArgs e)
{
string connectionString = "Data Source=DC-POR\\SQLEXPRESS;Initial Catalog=por_test;Integrated Security=True;";
SqlConnection con = new SqlConnection(connectionString);
con.Open();
string sql = "INSERT into tbl_persons (person_id,p_name,p_surname) VALUES (55,'TEST','test')";
SqlCommand cmd = new SqlCommand(sql);
cmd.Connection = con;
cmd.ExecuteNonQuery();
con.Close();
}
不应将值直接传递给insert语句。相反,您应该使用parameters.add来传递值。下面是执行SQL的更好方法
private void button1_Click(object sender, EventArgs e)
{
string connectionString = "Data Source=DC-POR\\SQLEXPRESS;Initial Catalog=por_test;Integrated Security=True;";
using(SqlConnection con = new SqlConnection(connectionString))
{
string sql = @"INSERT into tbl_persons (person_id, p_name, p_surname) VALUES (@param1, @param2, @param3)";
using(var cmd = new SqlCommand())
{
cmd.Connection = conn;
cmd.CommandType = CommandType.Text;
cmd.CommandText = sql;
cmd.Parameters.AddWithValue("@param1", 55);
cmd.Parameters.AddWithValue("@param2", 'TEST');
cmd.Parameters.AddWithValue("@param3", 'test1');
con.Open();
cmd.ExecuteNonQuery();
}
}
}
您需要调用
ExecuteNonQuery
来实际执行插入操作
代码应如下所示:
private void button1_Click(object sender, EventArgs e)
{
string connectionString = "Data Source=DC-POR\\SQLEXPRESS;Initial Catalog=por_test;Integrated Security=True;";
SqlConnection con = new SqlConnection(connectionString);
con.Open();
string sql = "INSERT into tbl_persons (person_id,p_name,p_surname) VALUES (55,'TEST','test')";
SqlCommand cmd = new SqlCommand(sql);
cmd.Connection = con;
cmd.ExecuteNonQuery();
con.Close();
}
不应将值直接传递给insert语句。相反,您应该使用parameters.add来传递值。下面是执行SQL的更好方法
private void button1_Click(object sender, EventArgs e)
{
string connectionString = "Data Source=DC-POR\\SQLEXPRESS;Initial Catalog=por_test;Integrated Security=True;";
using(SqlConnection con = new SqlConnection(connectionString))
{
string sql = @"INSERT into tbl_persons (person_id, p_name, p_surname) VALUES (@param1, @param2, @param3)";
using(var cmd = new SqlCommand())
{
cmd.Connection = conn;
cmd.CommandType = CommandType.Text;
cmd.CommandText = sql;
cmd.Parameters.AddWithValue("@param1", 55);
cmd.Parameters.AddWithValue("@param2", 'TEST');
cmd.Parameters.AddWithValue("@param3", 'test1');
con.Open();
cmd.ExecuteNonQuery();
}
}
}
您需要执行SQL命令。有关更多信息,请参阅任何介绍性书籍、教程或指南。另外,请阅读连接池部分,您需要调用
SqlCommand
,否则它实际上什么都不做!考虑阅读一些类似的代码-你的代码丢失了诸如<>代码>使用< /COD>或<代码>最后< /Cord>块,这可能导致连接泄漏和内存问题更久!您需要执行SQL命令。有关更多信息,请参阅任何介绍性书籍、教程或指南。另外,请阅读连接池部分,您需要调用SqlCommand
,否则它实际上什么都不做!考虑阅读一些类似的代码-你的代码丢失了诸如<>代码>使用< /COD>或<代码>最后< /Cord>块,这可能导致连接泄漏和内存问题更久!刚刚这样做了,但当我运行它时,我收到一条消息System.InvalidOperationException“ExecuteOnQuery:Connection属性尚未初始化。”您应该签出并停止使用.AddWithValue()
-它可能会导致意外和令人惊讶的结果…只是这样做了,但当我运行它时,我会收到一条消息System.InvalidOperationException“ExecuteOnQuery:Connection属性尚未初始化”。您应该签出并停止使用。AddWithValue()
-它可能会导致意外和令人惊讶的结果。。。