문제

I have converted my Datatable to json string use the following method...

public string GetJSONString(DataTable Dt)
{
    string[] StrDc = new string[Dt.Columns.Count];
    string HeadStr = string.Empty;
    for (int i = 0; i < Dt.Columns.Count; i++)
    {
        StrDc[i] = Dt.Columns[i].Caption;
        HeadStr += "\"" + StrDc[i] + "\" : \"" + StrDc[i] + i.ToString() + "¾" + "\",";
    }
    HeadStr = HeadStr.Substring(0, HeadStr.Length - 1);
    StringBuilder Sb = new StringBuilder();

    Sb.Append("{\"" + Dt.TableName + "\" : [");
    for (int i = 0; i < Dt.Rows.Count; i++)
    {
        string TempStr = HeadStr;
        Sb.Append("{");
        for (int j = 0; j < Dt.Columns.Count; j++)
        {
            if (Dt.Rows[i][j].ToString().Contains("'") == true)
            {
                Dt.Rows[i][j] = Dt.Rows[i][j].ToString().Replace("'", "");
            }
            TempStr = TempStr.Replace(Dt.Columns[j] + j.ToString() + "¾", Dt.Rows[i][j].ToString());
        }
        Sb.Append(TempStr + "},");
    }
    Sb = new StringBuilder(Sb.ToString().Substring(0, Sb.ToString().Length - 1));
    Sb.Append("]}");
    return Sb.ToString();
}

Is this fair enough or still there is margin for optimization to make it execute faster.... Any suggestion...

도움이 되었습니까?

해결책

다음을 시도하십시오

   <PublishingWebControls:RichImageField FieldName="InteralFieldName" runat="server"/>
.

페이지 레이아웃 상단에 다음이 있어야합니다.

<%@ Register TagPrefix="PublishingWebControls" Namespace="Microsoft.SharePoint.Publishing.WebControls" Assembly="Microsoft.SharePoint.Publishing, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
.

어떤 것을 알리려고했을 때 SharePoint 디자이너를 사용했습니다.SharePoint 사이트에 필요한 것을 배포하고 SPD에서 페이지 레이아웃을 열고 고급 모드로 편집 한 다음 페이지 필드의 도구 상자에서 콘텐츠 형식 필드를 볼 수 있습니다.페이지에서이를 드래그하여 어떻게 정의 해야하는지 알 수 있습니다.

다른 팁

There may well be ways of getting it to execute faster - but do you have any indication that you need it to execute faster? Do you have a good reason to believe this is a significant bottleneck in your code? If so, benchmark the code with some real data and profile the routine to work out where the time is going.

* 와일드 카드 문자는 접두사 일치 "와일드 *"가 아닌 "카드"에서만 지원됩니다.문서에 태그를 지정하기 위해 접근 방식을 확장하는 것이 좋습니다.SharePoint 메타 데이터를 사용하여 제목을 만드는 대신 문서에 태그를 지정하는 것이 좋습니다.예를 들어 문서, 제품, 변형, 보충, ID, 게시 된 언어 및 언어에 대한 필드를 추가하십시오.SharePoint 2013에서 사이트 열을 사용하는 경우 관리 속성으로 변환되고 KQL 또는 Refinement를 사용하여 검색을 수행 할 수 있습니다.

예 : 제품 : "superthing2000"변형 : "b"ID="13580"publisheddate> 08/01/2011

데이터 추출을 자동화하는 방법에는 몇 가지 방법이 있으며, 이벤트 수신기는이를 매우 쉽게 해결하기 위해 작성 될 수 있습니다.

Why do you think it needs optimization? Is it really slow on some DataTables? I'd just serialize DataTable with something like newton JSON serializer, if it's serializable at all.

Refactor your code, use a tool like ReSharper, JustCode etc to tidy it up a bit. Extract methods and use individual tests ( Test Driven Development-ish ) to find bottlenecks in your code and then tweak those.

But your first step should be: Refactor!

The problem with the code isn't speed, but that it's not cleaned up. I've done some clean-up, but you could probably do even more:

public string GetJSONString2(DataTable table)
{
    StringBuilder headStrBuilder = new StringBuilder(table.Columns.Count * 5); //pre-allocate some space, default is 16 bytes
    for (int i = 0; i < table.Columns.Count; i++)
    {
        headStrBuilder.AppendFormat("\"{0}\" : \"{0}{1}¾\",", table.Columns[i].Caption, i);
    }
    headStrBuilder.Remove(headStrBuilder.Length - 1, 1); // trim away last ,

    StringBuilder sb = new StringBuilder(table.Rows.Count * 5); //pre-allocate some space
    sb.Append("{\"");
    sb.Append(table.TableName);
    sb.Append("\" : [");
    for (int i = 0; i < table.Rows.Count; i++)
    {
        string tempStr = headStrBuilder.ToString();
        sb.Append("{");
        for (int j = 0; j < table.Columns.Count; j++)
        {
            table.Rows[i][j] = table.Rows[i][j].ToString().Replace("'", "");
            tempStr = tempStr.Replace(table.Columns[j] + j.ToString() + "¾", table.Rows[i][j].ToString());
        }
        sb.Append(tempStr + "},");
    }
    sb.Remove(sb.Length - 1, 1); // trim last ,
    sb.Append("]}");
    return sb.ToString();
}

I would suggest a different solution,if you are using .net 3.0 or 3.5

instead of doing this

  1. Convert datatable into xml
  2. use xmlserializer to convert the xml to your domain object
  3. Using JavaScriptSerializer(System.Web.Extensions.dll) to serialize the domain object to json string.
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top