我的代码有什么问题? [英] What is wrong with my codes?

查看:80
本文介绍了我的代码有什么问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从用户中提取admission_no,但我不想要phone_no,并且将其与test_test中的所有数据合并为admission_no.之后,我想将它们插入到合并表的一个表中.
示例表
用户
mission_no,phone_no
testo
subject_id,subject_name,admission_no,日期,时间,地点,seat_no
合并
subject_id,subject_name,admission_no,日期,时间,地点,seat_no
我应该如何在C#中执行此操作?请帮助.它非常紧急.
以下是我当前的密码..

I want to extract admission_no from users but I don''t want the phone_no and merge with admission_no with all the data from test. After that I want to insert them into one table which is the merge table.
Example Tables
users
admission_no,phone_no
testo
subject_id,subject_name,admission_no,date,time,venue,seat_no
merge
subject_id,subject_name,admission_no,date,time,venue,seat_no
HOW AM I SUPPOSE TO DO THAT IN C#?PLEASE HELP.ITS REALLY URGENT.
BELOW IS MY CURRENT CODES..

protected void Button1_Click(object sender, EventArgs e)
   {
     SqlConnection conn = new SqlConnection("Data Source= EN12-2-20-PC01\\SQLEXPRESS;" + "Initial Catalog=ssms;Integrated Security=SSPI");
     SqlDataAdapter adapSel;

     conn.Open();
     foreach (GridViewRow gvr in GridView2.Rows)   //loop through GridView
     {
       string viewqry = "INSERT INTO merge(subject_id,subject_name,admission_no,date,time,venue,seat_no) " + "(SELECT U.admission_no,T.subject_name,T.date,T.time,T.venue,T.seat_no " + "From users As U INNER JOIN testo As T ON U.admission_no = T.admission_no) VALUES (''" + gvr.Cells[0].Text + "'',''" + gvr.Cells[1].Text + "'',''" + gvr.Cells[2].Text + "'',''" + gvr.Cells[3].Text + "'',''" + gvr.Cells[4].Text + "'',''" + gvr.Cells[5].Text + "'',''" + gvr.Cells[6].Text + "'')";
       adapSel = new SqlDataAdapter(viewqry, conn);

       DataSet dsSel = new DataSet();
       adapSel.Fill(dsSel);
       GridView2.DataSource = dsSel;
       GridView2.DataBind();
       conn.Close();

    }

推荐答案

我的代码有什么问题?
很多!

首先开始:
1.您没有使用参数化查询
2.您正在ineach循环中一次又一次地设置gridview源,这没有任何意义
3. Gridview数据源需要是一个数据表,并且您可以设置一个数据集

最后但并非最不重要的一点,请确保所使用的查询有效.只需将原始查询放入SQL中,看是否可以,然后继续.
What is wrong with my codes?
Many!

To start with:
1. You are not using parameterized query
2. You are setting gridview source again and again inside the foreach loop that does not make any sense
3. Gridview datasource needs to be a datatable and you set a dataset

Last but not the least, do make sure that the query you are using works. Just put the raw query in SQL and see if it is ok and then move ahead.


您的查询似乎有问题.
您正在尝试使用-
INSERT INTO table (column1, column2, column3,...) SELECT column1, column2, column3,... FROM othertable VALUES (value1, value2, value3,...)
这完全无效.

您可以使用以下之一.
Your query seems to be so wrong.
You are trying to use -
INSERT INTO table (column1, column2, column3,...) SELECT column1, column2, column3,... FROM othertable VALUES (value1, value2, value3,...)
which is not valid at all.

You can use one of following.
INSERT INTO table VALUES (value1, value2, value3,...)
INSERT INTO table (column1, column2, column3,...) VALUES (value1, value2, value3,...)
INSERT INTO table SELECT column1, column2, column3,... FROM othertable
INSERT INTO table (column1, column2, column3,...) SELECT column1, column2, column3,... FROM othertable


这篇关于我的代码有什么问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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