SQLServer数据库:获取列标题(字段名)
发布时间:2020-12-12 13:29:51 所属栏目:MsSql教程 来源:网络整理
导读:写数据库之前最好先查看数据库字段名是否存在,否则很容易出现写数据库失败。 SqlConnection ^SqlServer_con;//SqlServerForm1(void){InitializeComponent();////TODO: 在此处添加构造函数代码//this-SqlServer_con = gcnew SqlConnection();//this-SqlServe
写数据库之前最好先查看数据库字段名是否存在,否则很容易出现写数据库失败。 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)即可获取到指定列的字段名称 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |