-2

我想为每一列赋予不同的 dwign 类(如在 CSS 中)。例如,用户名列以蓝色绘制,放大字体为 30px,将密码绘制为红色,并将其字体更改为“times new romans,12px”。我有以下cs代码与SQL中的数据库通信:

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”,但我不知道。

4

1 回答 1

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' 的定义”

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

2.第二次不知道“<”操作符

解决方案: 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;
    }
}
于 2013-03-28T08:20:44.783 回答