当前位置:编程学习 > C#/ASP.NET >>

大侠能否看看我这段代码,总是提示字段就那么有问题

  con.Open();
            string jnam = this.Tname.Text.Trim();
            string jid = "select jid from journal where jname='" + jnam + "'";
            //string jid2 = "select jid from journal where jid=((" + jid + ")-1)";
            string selectstring = "select fname from feature where jid='(" + jid + ")-1'";
            SqlCommand cmd = new SqlCommand(selectstring, con);
            string ss, insertstring;
            SqlCommand cmd2 = null;
            SqlDataAdapter adapter = new SqlDataAdapter(cmd);
            DataSet mySet = new DataSet();
            adapter.Fill(mySet);
            DataTable myTable = mySet.Tables[0];
            for (int i = 0; i < myTable.Rows.Count; i++)
            {
                ss = myTable.Rows[i]["fname"].ToString();
                insertstring = "insert into feature(jid,fname) values('" + jid + "','" + ss + "')";

                cmd2 = new SqlCommand(insertstring, con);
                cmd2.ExecuteNonQuery();
            }
            con.Close(); --------------------编程问答-------------------- ".....jid='(" + jid + ")-1'"
???

jid既然是数字,为什么还要加单引号???? --------------------编程问答-------------------- 那jname有问题嘛?
--------------------编程问答-------------------- string selectstring = "select fname from feature where jid='(" + jid + ")-1'";
为什么要括号? --------------------编程问答-------------------- 单引号是全角的,会出错 --------------------编程问答-------------------- string jid = "select jid from journal where jname='" + jnam + "'";
  //string jid2 = "select jid from journal where jid=((" + jid + ")-1)";
  string selectstring = "select fname from feature where jid='(" + jid + ")-1'";

---------------------------------

如果有多个值的话  应该用 in 而不是 =

单个值的话, + top 1 --------------------编程问答-------------------- 把这段 
 string jid = "select jid from journal where jname='" + jnam + "'";
  //string jid2 = "select jid from journal where jid=((" + jid + ")-1)";
  string selectstring = "select fname from feature where jid='(" + jid + ")-1'";

改为
    string jid = "declare @jid int   select @jid = jid from journal where jname='" + jnam + "'";
    jid += " select  fname from feature where jid= @jid - 1 ";
试试 --------------------编程问答--------------------   string jid = "select jid from journal where jname='" + jnam + "'";
  string selectstring = "select fname from feature where jid in(" + (jid-1) + ")";
  string selectstring = "select fname from feature where jid =" + (jid-1);

如果用=,就不需要(),如果用()那就是使用in
对于单一值尽量不要使用in。 --------------------编程问答--------------------

string jid ="select jid from journal where jname='"+ jnam +"'";
int id=jid-1;
string selectstring = "select fname from feature where jid="+ id;

insertstring = "insert into feature(jid,fname) values("+ jid +",'" + ss + "')";

--------------------编程问答-------------------- 括号,单引号要注意使用。。。还有吧sql语句写出来,看看有没有问题 --------------------编程问答-------------------- 呵呵 还要多练习练习哦 --------------------编程问答--------------------
引用 7 楼 zhoufoxcn 的回复:
  string jid = "select jid from journal where jname='" + jnam + "'";
  string selectstring = "select fname from feature where jid in(" + (jid-1) + ")";
  string selectstring = "select fname from fe……
--------------------编程问答-------------------- string selectstring = "select fname from feature where jid=(" + jid + "-1)";
补充:.NET技术 ,  ASP.NET
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,