SQLServer数据库:获取列标题(字段名)
扫描二维码
随时随地手机看文章
写数据库之前最好先查看数据库字段名是否存在,否则很容易出现写数据库失败。
SqlConnection ^SqlServer_con; //SqlServer Form1(void) { InitializeComponent(); // //TODO: 在此处添加构造函数代码 // this->SqlServer_con = gcnew SqlConnection(); //this->SqlServer_con->ConnectionString = "server=(local);database=temp;uid=sa;pwd=!QAZ@WSX#EDC"; this->SqlServer_con->ConnectionString = "server=(local);database=test;uid=sa;pwd=123456"; try { this->SqlServer_con->Open(); } catch (System::Exception^ e) { System::Windows::Forms::MessageBox::Show("链接数据库失败!", "错误", System::Windows::Forms::MessageBoxButtons::OK, System::Windows::Forms::MessageBoxIcon::Error); return; } SqlCommand ^SqlServer_cmd; //SqlServer SqlDataReader ^reader; try { SqlServer_cmd = gcnew SqlCommand("SELECT TOP 1 * FROM [sl651_2014]", this->SqlServer_con); //SqlServer reader = SqlServer_cmd->ExecuteReader(); for (int i = 0; i < reader->FieldCount;i ++) { this->textBox1->Text += ""+(i+1)+"t"+reader->GetName(i)->ToString()+"rn"; } } catch (System::Exception^ e) { } reader->Close(); this->SqlServer_con->Close(); }
使用 reader->GetName(i)即可获取到指定列的字段名称
reader->FieldCount即为列数量