我有以下函数执行查询并在成功时返回true,在失败时返回false.不,我想扩展该方法,以便在每个触发的插入查询中,类var insertId
包含最后插入的行的ID.
问题是它insertId
始终为0,所以不知何故executeScalar()
不返回ID.
有任何想法吗?或其他解决方案来获取最后一个插入查询的ID ....
public int insertId;
public bool executeCommand(string q) {
q += "; SELECT @@IDENTITY AS LastID";
bool retour = true;
SqlConnection myCon = new SqlConnection(Settings.DSN);
SqlCommand cmd = new SqlCommand(q, myCon);
try {
cmd.Connection.Open();
insertId = (int)cmd.ExecuteScalar();
if (insertId > 0) {
MessageBox.Show(insertId.ToString());
}
myCon.Close();
} catch (Exception ex) {
this.error = ex.Message;
retour = false;
}
return retour;
}
Run Code Online (Sandbox Code Playgroud) 此代码应该询问用户他们的名字,然后将其拆分到该空间.
它应该首先在变量中加上firstname,在变量lastname中加上最后一个名字
#include <iostream>
using namespace std;
int main()
{
char string[80];
char first[20];
char lastname[20];
bool f = true;
int c = 0;
cout << "Whats your Name? \n";
gets(string);
for(int i =0; i < strlen(string); i++){
if(string[i] == ' ') {
f = false;
c = 0;
}
if(f) {
first[c] = string[i];
} else if(!f) {
lastname[c] = string[i];
}
c++;
}
for(int i = 0; i < strlen(first); i++) {
cout << first[i] << "\n";
} …
Run Code Online (Sandbox Code Playgroud)