有一个表a:
id,int,自动获得
1,char
2,char
3,char
在C#中我有一个写到这个表的语句,
public Boolean execsql(string str)
    {
        SqlConnection con=new SqlConnection (ConfigurationManager .AppSettings ["constr"]);
        con.Open();
        string str="insert into a(1,2,3)values('"+ textbox1.text+"'.........)"
        SqlCommand cmd = new SqlCommand(str, con);
        try
        {
            cmd.ExecuteNonQuery();
            con.Close();        }
        catch(System .Exception e)
        {
            con.Close();
            return false;
        }
        return true;
    }
我现在返回的是布尔值,但是我想返回我刚才执行插入操作的哪个ID号怎么做啊?帮帮忙啊,各位老大

解决方案 »

  1.   

    我用的是  
    select max(id) from a where 1= textbox1.text……
      

  2.   

        id,int,自动获得    select max(id) from a 就可以,,,,得到最大的ID...
      

  3.   

    试试这个:
    string str = string.Format("insert into a(c1,c2,c3)values('{0}','{1}',{2}');SELECT @@IDENTITY",
      textbox1.Text.Trim(),textbox2.Text.Trim(),textbox3.Text.Trim()); 
    // ...
    int maxid = (int)cmd.ExecuteScalar();
      

  4.   

    好像是"insert into a(1,2,3)values('"+ textbox1.text+"'.........);select @@identity as 'identity'"
    然后 int i=int.parse(cmd.excuteScaler);
    没有做拼写检查,你自己应该可以对出来。
      

  5.   

        public int execsql(string str) 
        { 
          string str = string.Format("insert into a(c1,c2,c3)values('{0}','{1}',{2}');SELECT @@IDENTITY",
            textbox1.Text.Trim(),textbox2.Text.Trim(),textbox3.Text.Trim()); 
          using (SqlConnection con=new SqlConnection (ConfigurationManager .AppSettings ["constr"]))
          {  
            con.Open(); 
            SqlCommand cmd = new SqlCommand(str, con); 
            return (int)cmd.ExecuteScalar();
          }
        } 
      

  6.   

    你的T-Sql语句后面加上 ;select @@identity  string str="insert into a(1,2,3)values('"+ textbox1.text+"'.........)"
      str+=";select @@identity"
            SqlCommand cmd = new SqlCommand(str, con); 
      

  7.   

    string str1 = string .Format ("insert into [file](filesender,filetitle,filetime,filecontent,path,examine,filename)values('我','"+TextBox2 .Text +"','"+DateTime .Today .ToString ()+"','"+TextBox3 .Text +"','"+path +"','未接受','"+ss +"');SELECT @@IDENTITY"); 
    SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["constr"]);
            con.Open();
            SqlCommand cmd = new SqlCommand(str, con);
            return (int)cmd.ExecuteScalar();
    提示错误:用户未处理代码InvalidCastException 指定的转换无效。
      

  8.   


    string str1 = string .Format ("insert into [file] (filesender,filetitle,filetime,filecontent,path,examine,filename)values(' 我 ','"+TextBox2 .Text +"','"+DateTime .Today .ToString ()+"','"+TextBox3 .Text +"','"+path +"',' 未接受','"+ss +"');SELECT @@IDENTITY"); 
    SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["constr"]);
            con.Open();
            SqlCommand cmd = new SqlCommand(str, con);
           int Num= com.ExecuteNonQuery();
    return Num
      

  9.   

    去最后一条记录可以这样啊:select gpId from (select rownum id,t_gps.* from t_gps) a where a.id = (select count(*) from t_gps) 取最后一条记录
      

  10.   

    string str1 = string .Format ("insert into [file] (filesender,filetitle,filetime,filecontent,path,examine,filename)values(' 我 ','"+TextBox2 .Text +"','"+DateTime .Today .ToString ()+"','"+TextBox3 .Text +"','"+path +"',' 未接受','"+ss +"');SELECT @@IDENTITY");  
    SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["constr"]); 
            con.Open(); 
            SqlCommand cmd = new SqlCommand(str, con); 
           int Num= com.ExecuteNonQuery(); 
    return Num 
    得到的NUM是1啊,不符合啊,每次都是1啊
      

  11.   

    你的 id,int,自动获得 设成自动编号、主键
      

  12.   

    写存储过程.里面返回@@IDENTITY
      

  13.   

    数据库里写存储过程 返回@@IDENTITY全局变量