C#程序读取数据库中包含null的列的值

 private void btn2_Click(object sender, RoutedEventArgs e)
        {
            using (SqlConnection conn = new SqlConnection("Data Source=.;Initial Catalog=Test20140531;User ID=sa;Password=123"))
            {
                conn.Open();
                using (SqlCommand cmd2 = conn.CreateCommand())
                {
                    cmd2.CommandText = "select * from T_Student";
                    using (SqlDataReader reader = cmd2.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            //string Name = reader.GetString(1);
                            //MessageBox.Show("姓名为:" + Name);


                            //int Age = reader.GetInt32(2);
                            //MessageBo

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C#,可以使用Microsoft.Office.Interop.Word和ADO.NET库来实现从数据库读取数据,并根据数据生成Word文档的打印和预览。下面是一个简单的示例代码,可以从数据库读取数据,将数据插入到Word文档,然后预览并打印出来: ```csharp using Microsoft.Office.Interop.Word; using System.Data.SqlClient; // 声明Word应用程序对象 Application wordApp = new Application(); Document wordDoc = null; // 声明数据库连接字符串 string connString = "Data Source=yourServer;Initial Catalog=yourDatabase;User ID=yourUsername;Password=yourPassword"; // 声明SQL查询语句 string sql = "SELECT * FROM yourTable"; try { // 打开数据库连接 using (SqlConnection conn = new SqlConnection(connString)) { conn.Open(); // 执行SQL查询 using (SqlCommand cmd = new SqlCommand(sql, conn)) { SqlDataReader reader = cmd.ExecuteReader(); // 创建一个新的Word文档 wordDoc = wordApp.Documents.Add(); // 将查询结果插入到Word文档 while (reader.Read()) { wordDoc.Content.Text += reader.GetString(0) + "\t" + reader.GetString(1) + "\r\n"; } reader.Close(); } // 预览Word文档 wordDoc.PrintPreview(); // 打印Word文档 wordDoc.PrintOut(); // 关闭Word文档 wordDoc.Close(false); } } catch (Exception ex) { Console.WriteLine("Error: " + ex.Message); } finally { // 关闭Word应用程序 wordApp.Quit(false); } ``` 注意:在使用ADO.NET库时,需要确保你的计算机上已经安装了适当的数据库驱动程序

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值