用visual studio创建一个简单的查询窗口,查询本机sqlserver 255
输出数据库的记录,该怎么写代码?跪求解决定重谢,本人小白数据库office表hospital表字段包括(id,hospitalname,hospitalip,hospit...
输出数据库的记录,该怎么写代码?跪求解决定重谢,本人小白数据库office 表hospital 表字段包括(id,hospitalname,hospitalip,hospitalpsw)用windows身份验证通过在label中输入查询条件名称 点击查询按钮 把查询结果输出在截图3位置程序代码:using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;using System.Data.SqlClient;namespace WindowsFormsApplication1{public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void textBox1_TextChanged(object sender, EventArgs e) { } private void label2_Click(object sender, EventArgs e) { } private void button1_Click(object sender, EventArgs e) { } private void textBox2_TextChanged(object sender, EventArgs e) { } }}
把这样的结果展示在截图3出就行 展开
把这样的结果展示在截图3出就行 展开
展开全部
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Data;
using System.Data.SqlClient;
namespace 测试导出文件
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void button1_Click(object sender, RoutedEventArgs e)
{
SqlConnection con = new SqlConnection();
con.ConnectionString = "server=数据库ip;database=数据库名;uid=登录名;
pwd=密码";
con.Open();//创建打开链接
SqlCommand com = new SqlCommand();
com.Connection = con;
com.CommandType = CommandType.Text;
com.CommandText = "sql语句可以调用存储过程";
SqlDataReader dr = com.ExecuteReader();//执行SQL语句
for (int y = 0; y < dr.FieldCount ; y++)
{
cs11 = cs11 + dr.GetName(y) + "\t";//表头
}
for (int x = 0; x < dr.FieldCount; x++)
{
//cs11 = cs11 + dr.GetName(x) + " ";//.ToString() + new string(' ', (max-x1)*2)+" ";
cs4 = cs4 + String.Format("{0}", dx[x]) + "\t";// + new string(' ', (max - x2) * 2) + " ";
}
// System.IO.File.WriteAllText(@"D:\测试\" + "第一条" + ".bdf", cs6, Encoding.UTF8);
while (dr.Read())
{
//cs5 = String.Format("{0}", dr[0]);
//cs4 = cs4 + "空格";
//System.IO.File.WriteAllText(@"D:\测试\"+cs5+".bdf", cs11 + "\r\n" + cs4, Encoding.UTF8);
cs5 = "";
cs5 = String.Format("{0}_{1}_{2}", dr[9], dr[10], dr[11]);
if (cs5 == cs6)
{
cs4 = cs4 + "\r\n";
}
else
{
cs4 = "";
}
cs6 = cs5;
for (int x = 0; x < dr.FieldCount; x++)
{
//cs11 = cs11 + dr.GetName(x) + " ";//.ToString() + new string(' ', (max-x1)*2)+" ";
cs4 = cs4 + String.Format("{0}", dr[x]) + "\t";// + new string(' ', (max - x2) * 2) + " ";
}
System.IO.File.WriteAllText(@"D:\测试\" + cs5 + ".bdf", cs11 + "\r\n" + cs4, Encoding.UTF8);
//System.IO.File.WriteAllText(@"D:\测试\" + dr.Read() + ".TXT", "123", Encoding.UTF8);
// string cs = dr.GetString(0);
//string strTest = cs4;
//System.IO.File.WriteAllText(@"D:\测试\测试.bdf", strTest, Encoding.UTF8);
}
dr.Close();//关闭执行
con.Close();//关闭数据库
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询