数据库表中每列的不同CSS类

时间:2013-03-28 06:37:38

标签: c# sql css

我想给每个列添加不同的dwign类(如CSS)。例如,用蓝色绘制的用户名列为30px的放大字体,用红色绘制密码并将其字体更改为'times new romans, 12像素”。  我有以下cs代码与SQL中的DB进行通信:

using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public class MyAdoHelper
{
public static SqlConnection ConnectToDB(string fileName)
{
    string path = HttpContext.Current.Server.MapPath("App_Data/");
    path += "Database.mdf";

    string connString = @"Data Source=.\SQLEXPRESS;AttachDbFileName=" +
        path + ";Integrated Security=True;User Instance=True";

    SqlConnection conn = new SqlConnection(connString);

    return conn;

}
public static void DoQuery(string fileName, string sql)
{
    SqlConnection conn = ConnectToDB(fileName);
    conn.Open();
    SqlCommand com = new SqlCommand(sql, conn);
    com.ExecuteNonQuery();
    conn.Close();
}
public static DataTable ExecuteDataTable(string fileName, string sql)
{
    SqlConnection conn = ConnectToDB(fileName);
    conn.Open();
    DataTable dt = new DataTable();
    SqlDataAdapter tableAdapter = new SqlDataAdapter(sql, conn);
    tableAdapter.Fill(dt);
    return dt;
}
public static string printDataTable(string fileName, string sql)
{
    DataTable dt = ExecuteDataTable(fileName, sql);
    string printStr = "<table border='1'>";
    foreach (DataRow row in dt.Rows)
    {
        printStr += "<tr>";
        foreach (object myItemArray in row.ItemArray)
        {
            printStr += "<td>" + myItemArray.ToString() + "</td>";
        }
        printStr += "</tr>";
    }
    printStr += "</table>";
    return printStr;
}

public static bool IsExist(string fileName, string sql)
{
    bool found;
    SqlConnection conn = ConnectToDB(fileName);
    conn.Open();
    SqlCommand com = new SqlCommand(sql, conn);
    SqlDataReader data = com.ExecuteReader();
    found = (bool)data.Read();
    conn.Close();

    return found;
}
public static int RowsAffected(string fileName, string sql)
{
    SqlConnection conn = ConnectToDB(fileName);
    conn.Open();
    SqlCommand com = new SqlCommand(sql, conn);
    int rowsA = com.ExecuteNonQuery();
    conn.Close();

    return rowsA;
}
}

我想改变“printdataTable”,但我不知道。

1 个答案:

答案 0 :(得分:2)

添加包含您的班级名称的字符串:

string classUserName ="class='username'";
string classPasswords ="class='username'";

您应该更改此部分:

 foreach (object myItemArray in row.ItemArray)
 {
    printStr += "<td>" + myItemArray.ToString() + "</td>";
 }

类似

foreach (DataColumn dataCol in row.Table.Columns)
{
   printStr += String.Format("<td {0}>" + row[dataCol].ToString() + "</td>", dataCol.ColumnName == "UsersColumnName" ? classUserName  : classPasswords);
}

之类的东西(如果你想使用ItemArray)

for (int i=0;i<row.ItemArray.Length;i++)
{
   //if column 0 of the row (0 element of ItemArray) is the user name column
   printStr += String.Format("<td {0}>" + row.ItemArray[i].ToString() + "</td>", i == 0 ? classUserName  : classPasswords);
}

修改

我纠正了这两条建议:

1。在第一个错误是“foreach语句无法对类型'System.Data.DataSet'的变量进行操作,因为'System.Data.DataSet'不包含'GetEnumerator'的定义““

解决方案:需要“Columns”属性才能正确迭代。

2。在第二部分,它不知道“&lt;”操作

解决方案:row.ItemArray有一个Length属性,而不是Count一个。

编辑2:

关于您的第二条评论,如果您需要添加其他列,则可以执行以下操作:

string classEmails ="class='email'";

   for (int i=0;i<row.ItemArray.Length;i++)
   {
     //if column 0 of the row (0 element of ItemArray) is the user name column
     //if column 1 of the row (1st element of ItemArray) is the password column
     //if column 2 of the row (2nd element of ItemArray) is the email column
     printStr += String.Format("<td {0}>" + row.ItemArray[i].ToString() + "</td>", i == 0 ?    classUserName  : (i == 1 ?  classPasswords : classEmails));
   }

如果数据源中的列数多于三元表达式中的项目,则电子邮件类(三元表达式中的最后一个元素)将应用于所有其他列。

在这种情况下,您应该使用开关,以允许其他列不被格式化:

for (int i=0;i<row.ItemArray.Length;i++)
{
switch(i){
        case 0:
        //if column 0 of the row (0 element of ItemArray) is the user name column
        printStr += String.Format("<td {0}>" + row.ItemArray[i].ToString() + "</td>", classUserName);
        break;
        case 1:
        //if column 1 of the row (1st element of ItemArray) is the password column
        printStr += String.Format("<td {0}>" + row.ItemArray[i].ToString() + "</td>", classPasswords);
        break;
        case 2:
        //if column 2 of the row (2nd element of ItemArray) is the email column
        printStr += String.Format("<td {0}>" + row.ItemArray[i].ToString() + "</td>", classEmails);
        break;
        default:
        //any column you do not want to style
        printStr += "<td>" + row.ItemArray[i].ToString() + "</td>";
        break;
    }
}