将数据插入gridview [英] inserting data into gridview

查看:72
本文介绍了将数据插入gridview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

SqlConnection objCon = new SqlConnection(@Data Source = hyd-npulivarthy; Initial Catalog = projecthotel; Integrated Security = True);



SqlCommand objCmd = new SqlCommand();

objCmd.Connection = objCon;



string query =(insert into Reservation(@Code,@ Status,@客人姓名,@ Guest电子邮件,@ Guest Phone No,@ Checkin,@ Check Out,@ Adult No,@ Child No,@ Infant No,@ Net Total,@ Discount,@ Tax,@ Total,@ Paid)值( '+ textBox1.Text +','+ comboBox2.SelectedValue +','+ textBox1.Text +','+ textBox1.Text +','+ textBox1.Text +','+ textBox1 .Text +','+ textBox1.Text +'));



objCon.Open();

int j = objCmd.ExecuteNonQuery();

if(j> 0)

{



MessageBox.Show( Record Inserted,www.codingresolved.com);

dataGridVie w1.Rows.Clear();

}



objCon.Close();



Form1 form1 = new Form1();

form1.Show();





出现在

)值中的错误('+ textBox1.Text +',''+ comboBox2.SelectedValue +','+ textBox1.Text +','+ textBox1.Text + ','+ textBox1.Text +','+ textBox1.Text +','+ textBox1.Text +')); ..

你能不能纠正

SqlConnection objCon = new SqlConnection(@"Data Source=hyd-npulivarthy;Initial Catalog=projecthotel;Integrated Security=True");

SqlCommand objCmd = new SqlCommand();
objCmd.Connection = objCon;

string query = ("insert into Reservation (@Code, @Status,@Guest Name,@Guest Email,@Guest Phone No,@Checkin,@Check Out,@Adult No,@Child No,@Infant No,@Net Total,@Discount,@Tax,@Total,@Paid")values( '" +textBox1.Text+ "',"' + comboBox2.SelectedValue + "','" +textBox1.Text+ "','" +textBox1.Text+ "','" +textBox1.Text+ "','" +textBox1.Text+ "','" +textBox1.Text+ "')");

objCon.Open();
int j = objCmd.ExecuteNonQuery();
if (j > 0)
{

MessageBox.Show("Record Inserted","www.codingresolved.com");
dataGridView1.Rows.Clear();
}

objCon.Close();

Form1 form1 = new Form1();
form1.Show();


The error comming in
)values( '" +textBox1.Text+ "',"' + comboBox2.SelectedValue + "','" +textBox1.Text+ "','" +textBox1.Text+ "','" +textBox1.Text+ "','" +textBox1.Text+ "','" +textBox1.Text+ "')");..
can u plz rectify

推荐答案

你应该重写整个sql 使用参数化查询来防止SQL Server中的SQL注入攻击 [ ^ ]

这也将避免凌乱的令人困惑的引号问题。
You should rewrite you whole sql Using Parameterized queries to prevent SQL Injection Attacks in SQL Server[^]
That will also avoid the messy and confusing quotes problem.


检查no.of列和值,你的传递是相同的数量,都是字符串数据类型?



试试这个



string query =插入预订(@Code,@ Status,@ Guest Name,@ Guest Email,@ Guest Phone No)值('+ textBox1.Text +','+ comboBox2.SelectedValue +','+ textBox1.Text +','+ textBox1.Text +','+ textBox1.Text +');
Check the no.of columns and values your passing are same count and all are string datatype?

try this

string query = "insert into Reservation (@Code, @Status,@Guest Name,@Guest Email,@Guest Phone No)values( '" +textBox1.Text+ "',"' + comboBox2.SelectedValue + "','" +textBox1.Text+ "','" +textBox1.Text+ "','" +textBox1.Text+ "')";


你遗漏了值和组合框附近的一些报价



试试这个

You are missing some quotes near values and combobox

try this
string query = ("insert into Reservation (@Code, @Status,@Guest Name,@Guest Email,@Guest Phone No,@Checkin,@Check Out,@Adult No,@Child No,@Infant No,@Net Total,@Discount,@Tax,@Total,@Paid)+values( '" +textBox1.Text+ "','" + comboBox2.SelectedValue + "','" +textBox1.Text+ "','" +textBox1.Text+ "','" +textBox1.Text+ "','" +textBox1.Text+ "','" +textBox1.Text+ "')");


这篇关于将数据插入gridview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆